Ternary operator associativity
Tim Chase
python.list at tim.thechases.com
Thu Mar 6 13:59:58 EST 2014
On 2014-03-06 03:34, candide wrote:
> According to the official documentation, the ternary operator has
> left-to-right associativity
>
> >>> left_to_right = (0 if 1 else 0) if 0 else 1
> >>> right_to_left = 0 if 1 else (0 if 0 else 1)
I'd never want to rely on my own ability to remember the language
spec, so I strongly advocate for making it explicit with parens
regardless of what the language defines. And that's if I ever created
such a mess in the first place. If you have more than one pair of
conditional expressions in a single assignment, I'd suggest that it's
a code-smell that could use refactoring for clarity/disambiguity.
-tkc
More information about the Python-list
mailing list