conditional expressions (RE: Loop-and-a-half (Re: Curious assignment behaviour))

Michael Abbott michael at
Mon Oct 15 08:54:00 CEST 2001

"Rainer Deyke" <root at> wrote in
news:ooty7.61655$w62.37584078 at 

> Actually, it would probably be possible to make the parens optional in
> all cases except where 'if' is the first token of the expression.
> x = if a then b else c # legal
> if a then b else c     # syntax error
> The required changes in the grammar are quite small:
>   1. Replace all rhs instances of "testlist" with "any_expression",
>    *except* the first one in "expr_stmt".
>   2. Add the following production: "any_expression: testlist
>   | 'if' any_expression 'then' any_expression 'else' any_expression".

Yes!  And in this particular case it really doesn't matter anyway, since an 
isolated if expression is indistinguishable in effect from an isolated if 

Oh, while we're being concrete about the syntax, don't forget to allow elif 
in conditional expressions!

More information about the Python-list mailing list