You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Context of documentation for conditional expressions #65058
The documentation for conditional expressions (c_exps) http://docs.python.org/3/reference/expressions.html#conditional-expressions
and the later sections on evaluation order and operator precedence have 3 inconsistencies with each other. I believe the latter (the 'context' of the title) were not properly adjusted with the addition of the former to match the exceptional behavior of c_exps.
Associativily-grouping: I believe the grammar line
conditional_expression ::= or_test ["if" or_test "else" expression]
makes c_exps group right to left, as in C. Something like
conditional_expression ::= expression ["if" or_test "else" or_test]
would have the opposite effect. The following examples posted on python-list by 'candide' illustrate and verify.
"Operators in the same box group left to right (except for comparisons, including tests, which all have the same precedence and chain from left to right — see section Comparisons — and exponentiation, which groups from right to left)."
should have the last part revised to
"and exponentiation and conditional expressions, which group from right to left".
Perhaps a sentence and example should also be added to the C-E section also.
I think "Except for conditional expressions, python evaluates expressions from left to right." gives too much weight to the exception.
"Python evaluates expressions from left to right (except for conditional expressions)." is a bit more awkward, but, appropriately, makes the exception more like a footnote.
Precedence: "Conditional expressions (sometimes called a “ternary operator”) have the lowest priority of all Python operations." versus the table, which lists lambda as lowest priority. Should 'except for lambda' be added to the end of the sentence? Should the top two lines of the table be reversed? I do not know which is true, or perhaps neither. I get the impression from the grammar and the discussion in PEP-308 that there are sort-of at the same level. Raymond, I add you as a PEP author particularly for this question.
In #162167 the "(except for conditional expressions)" wording for "Evaluation order" was dropped after review, since it would be incomplete (comprehensions are not evaluated left to right either). Nowt he PR only documents the grouping.
Activity
terryjreedy commentedon Mar 6, 2014
The documentation for conditional expressions (c_exps)
http://docs.python.org/3/reference/expressions.html#conditional-expressions
and the later sections on evaluation order and operator precedence have 3 inconsistencies with each other. I believe the latter (the 'context' of the title) were not properly adjusted with the addition of the former to match the exceptional behavior of c_exps.
conditional_expression ::= or_test ["if" or_test "else" expression]
makes c_exps group right to left, as in C. Something like
conditional_expression ::= expression ["if" or_test "else" or_test]
would have the opposite effect. The following examples posted on python-list by 'candide' illustrate and verify.
This sentence in the Operator Precedence section
http://docs.python.org/3/reference/expressions.html#operator-precedence
"Operators in the same box group left to right (except for comparisons, including tests, which all have the same precedence and chain from left to right — see section Comparisons — and exponentiation, which groups from right to left)."
should have the last part revised to
"and exponentiation and conditional expressions, which group from right to left".
Perhaps a sentence and example should also be added to the C-E section also.
"Python evaluates expressions from left to right."
http://docs.python.org/3/reference/expressions.html#evaluation-order
wrong as is, without noting the exception of c_exps.
I think "Except for conditional expressions, python evaluates expressions from left to right." gives too much weight to the exception.
"Python evaluates expressions from left to right (except for conditional expressions)." is a bit more awkward, but, appropriately, makes the exception more like a footnote.
serhiy-storchaka commentedon Jun 23, 2021
LGTM
LGTM. It would be nice to add also examples for conditional and assignment expressions.
Yes, lambda and assignment expressions have lower priority.
Revert "pythongh-65058: Note that conditional expressions are not eva…
pythongh-65058: Use True/False and spam/eggs/ham in the grouping example
lul-cas commentedon Nov 18, 2026
In #162167 the "(except for conditional expressions)" wording for "Evaluation order" was dropped after review, since it would be incomplete (comprehensions are not evaluated left to right either). Nowt he PR only documents the grouping.
Should point 2 be considered resolved?