On 3/6/2014 6:34 AM, candide wrote:
According to the official documentation, the ternary operator has
left-to-right associativity :

The proper terms is 'conditional expression', which goes back to "The C Programming Language" (K&R). There are many unary operators, many binary operators, and there could be other ternary operators.

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

Nevertheless, the ternary operator grouping seems to be from right to
left, compare :

p = 0 if 1 else 0 if 0 else 1 p
0
left_to_right = (0 if 1 else 0) if 0 else 1
>>>> right_to_left = 0 if 1 else (0 if 0 else 1)
>>>> p == left_to_right
False
p == right_to_left
True

This behavior is specified in the grammar as given in the C-E section.

The doc is also inconsistent about evaluation order and precedence. I opened http://bugs.python.org/issue20859 .

--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to