For review: PEP 308 - If-then-else expression

Erik Max Francis max at alcyone.com
Tue Feb 11 02:13:39 EST 2003


Paul Moore wrote:

> Consider
> 
>     if cond: f(0) else: f(1)
> 
> Currently this is invalid Python (you need a newline before the "else"
> to make it a valid if statement). With an if expression as you
> propose, it becomes a perfectly valid expression statement, which
> makes one of two function calls.

This is the Corner Case that has been discussed at some length; it's the
place where the expression and the statement meet in the middle.  I
don't see any reason to worry about this case, since it really is
degenerate; leaving the parser the way it currently is and letting it
parse as a statement (and thus illegal) seems fine to me; if you really
_meant_ the conditional operator, you can surround it with parentheses,
or split it over lines and make it a real statement.  Neither requires
any changes to the parser, other than the addition of the conditional
operator itself (as an expression, not a statement).

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ There I was / There I was / Splitting atoms with my desire
\__/ Lamya
    Computer science / http://www.alcyone.com/max/reference/compsci/
 A computer science reference.




More information about the Python-list mailing list