PEP 308: Precedence of "a then b else c"

Chermside, Michael mchermside at ingdirect.com
Thu Feb 13 09:33:34 EST 2003


> What's the precedence of this thing?
> 
> For example, what do each of these mean:
>    a + b then c else d
>    a then b else c + d
>    a and b then c else d
>    a then b else c and d
>    a, b then c else d
>    a then b else c, d
>    lambda: a then b else c

Based on discussion on python-dev, I believe the intent is to
have a precedence just below that of lambda. In other words,
all operators bind tighter than the conditional expression
other than lambda, which is looser. Thus, your examples above
would be equivalent to the following:

    (a + b) then c else d
    a then b else (c + d)
    (a and b) then c else d
    a then b else (c and d)
    (a, b) then c else d
    a then b else (c, d)
    lambda: (a then b else c)

Of course, anytime you think someone might get confused, it's
good STYLE (though perhaps not syntactically required) to add
extra parentheses to clarify. Someone else please correct me 
if I'm wrong on any of these examples.

-- Michael Chermside





More information about the Python-list mailing list