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

Rainer Deyke root at
Mon Oct 15 06:14:12 CEST 2001

"Tim Peters" < at> wrote in message
news:mailman.1003113854.26899.python-list at
> You're apparently responding to an edited reply, and didn't read the
> from its start:  surrounding parens are required else you don't get this
> form of conditional expression at all.  It's not a question of whether to
> require surrounding parens or not; the latter isn't an option, due to the
> limitations of one-token lookahead parsing (read the thread from the

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

(This still doesn't allow unadorned conditional expressions in places where
"testlist" is not used, such as function arguments.)

Rainer Deyke (root at
Shareware computer games           - 
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor

More information about the Python-list mailing list