jeudi 6 mars 2014

Ternary operator associativity topic




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






Aucun commentaire:

Enregistrer un commentaire