pythonpython-3.xconditional-operatoroperator-precedence

How do conditional expressions group from right to left?


I checked python operator precedence (This one grammar is more detailed and more appropriate for the actual Python implementation)

Operators in the same box group left to right (except for exponentiation and conditional expressions, which group from right to left).

** Exponentiation [5]

if – else Conditional expression

I can understand exponentiation that 2**3**2 is equal to 2**(3**2). But Conditional expression

conditional_expression ::= or_test ["if" or_test "else" expression]

is not one binary operator. I can't give one similar example as **. Could you give one example of "group from right to left" for if Conditional expression?


Solution

  • a if b else c if d else e

    means

    a if b else (c if d else e)

    and not

    (a if b else c) if d else e