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

Michael Abbott michael at rcp.co.uk
Mon Oct 15 08:54:00 CEST 2001


"Rainer Deyke" <root at rainerdeyke.com> wrote in
news:ooty7.61655$w62.37584078 at news1.denver1.co.home.com: 

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

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