[issue20859] Context of documentation for conditional expressions

Terry J. Reedy report at bugs.python.org
Thu Mar 6 19:48:00 CET 2014


New submission from Terry J. Reedy:

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.


1. 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.

>>> 0 if 1 else 0 if 0 else 1
0
>>> 0 if 1 else (0 if 0 else 1)
0
>>> (0 if 1 else 0) if 0 else 1
1

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.


2. Evaluation Order: Condition expressions evaluate the middle subexpession first, making 
"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.


3. 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 PEP308 that there are sort-of at the same level. Raymond, I add you as a PEP author particularly for this question.

----------
messages: 212835
nosy: rhettinger, terry.reedy
priority: normal
severity: normal
stage: needs patch
status: open
title: Context of documentation for conditional expressions
type: behavior
versions: Python 2.7, Python 3.3, Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20859>
_______________________________________


More information about the Python-bugs-list mailing list