Ternary operator associativity

candide c.candide at laposte.net
Thu Mar 6 06:34:49 EST 2014


According to the official documentation, the ternary operator has left-to-right associativity :

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



More information about the Python-list mailing list