PYT - The expressions described in the Python language reference yield only boolean values

vanyp vanyp at skynet.be
Sat Feb 19 17:28:28 EST 2022


*I am trying to learn Python from the grammar given in the Python 
language reference and I am surprised.*

*Lets start here:*

*"*
*6.3.4. Calls*

A call calls a callable object (e.g., a function 
<https://docs.python.org/3/glossary.html#term-function>) with a possibly 
empty series of arguments 
<https://docs.python.org/3/glossary.html#term-argument>:

*call *::= |primary 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-primary>|"(" [|argument_list 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-argument_list>|[","] | |comprehension 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-comprehension>|] ")"
*argument_list *::= |positional_arguments 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-positional_arguments>|["," |starred_and_keywords 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-starred_and_keywords>|]
["," |keywords_arguments 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-keywords_arguments>|]
| |starred_and_keywords 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-starred_and_keywords>|["," |keywords_arguments 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-keywords_arguments>|]
| |keywords_arguments 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-keywords_arguments>|
*positional_arguments*::= positional_item ("," positional_item)*
*positional_item *::= |assignment_expression 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-assignment_expression>|| "*" |expression 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-expression>|
*"*

*continued by:*
*"*
*6.12. Assignment expressions*
*assignment_expression*::= [|identifier 
<https://docs.python.org/3/reference/lexical_analysis.html#grammar-token-python-grammar-identifier>|":="] |expression 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-expression>|
*"*

*Now I look at the following productions:*

*"*
*6.13. Conditional expressions*
*conditional_expression*::= |or_test 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-or_test>|["if" |or_test 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-or_test>|"else" |expression 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-expression>|]
*expression *::= |conditional_expression 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-conditional_expression>|| |lambda_expr 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-lambda_expr>|
*"*

*The first or_test is strange, I assume it should be replaced by expression.*

*But even so I think that the only ways out of the recursion are the 
or_test or the lambda_expr.*

*And by the grammar, those evaluate to booleans as follows:*


*"*
*6.14. Lambdas*
*lambda_expr*::= "lambda" [|parameter_list 
<https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-parameter_list>|] ":" |expression 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-expression>|
*"*

*and I conclude that the only way out of that branch is also or_test.*

*I then look at or_test:*

*"*
*6.11. Boolean operations*
*or_test *::= |and_test 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-and_test>|| |or_test 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-or_test>|"or" |and_test 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-and_test>|
*and_test*::= |not_test 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-not_test>|| |and_test 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-and_test>|"and" |not_test 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-not_test>|
*not_test*::= |comparison 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-comparison>|| "not" |not_test 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-not_test>|
*"*

*and the ony way out is comparison, which by the semantics should return 
a boolean.*

*Looking at comparison gives:*

*"*
**6.10. Comparisons**
*comparison *::= |or_expr 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-or_expr>|(|comp_operator 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-comp_operator>||or_expr 
<https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-or_expr>|)*
*comp_operator*::= "<" | ">" | "==" | ">=" | "<=" | "!="
| "is" ["not"] | ["not"] "in"

Comparisons yield boolean values: |True| or |False|. Custom /rich 
comparison methods/ may return non-boolean values. In this case Python 
will call |bool() 
<https://docs.python.org/3/library/functions.html#bool>| on such value 
in boolean contexts.

*"*

*By now I think that we are firmly stuck in boolean operations. Which 
would mean that any expression always returns a boolean value.*

*Where did I, or the language reference, go wrong?*

*Sincerely,*

*Pierre Van Nypelseer*


More information about the Python-list mailing list