Another conditional expression candidate (PEP 308)

Erik Max Francis max at alcyone.com
Sun Feb 9 22:34:39 EST 2003


Andrew Koenig wrote:

> I don't see any reason for the parens to be mandatory.
> In particular, I don't see any reason why I should not be
> able to write
> 
>         foo(if a > 0: 'positive' elif a == 0: 'zero' else: 'negative')
> 
> or, for that matter,
> 
>         fact = lambda n: if n == 0: 1 else: n*fact(n-1)
> 
> I don't think there are any ambiguities here.

The only issue I see here parser wise is that if someone's wacky enough
to use the expression as a standalone statement (as mentioned in the PEP
itself, albeit with the `if C then x else y' form, not the `if C: x
else: y' form we're talking about here, it'd take a bit of lookahead to
realize that what you've got is an expression, not a statement. 
Requiring parentheses would eliminate that case faster without the
lookahead, since you damn sure can't have an if statement following an
open parenthesis.  As I mentioned, I don't know enough about the parser
internals to know whether that would really be a significant benefit.

The more I look at it, the more I like it.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Patiently, I'm still / Holding out until
\__/ Sandra St. Victor
    Church / http://www.alcyone.com/pyos/church/
 A lambda calculus explorer in Python.




More information about the Python-list mailing list