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

Erik Max Francis max at
Mon Feb 10 05:27:28 CET 2003

holger krekel wrote:

> this makes the parsing process ambigous because an expression
> can also be a statement.

Well, it's unambiguous, but it could require some extra lookahead.  I'm
no Python parser expert, but it seems to me the only case this happens
is when the conditional expression is used as a standalone statement,

	if debug: sys.stderr.write('got here\n') else: None

which I must admit is a little perverse in and of itself, particularly
as an example -- but Guido used it in the PEP so I will use it here too.
(No saying, "Duh, just remove the "else: None"; I know that perfectly
well, and so did Guido.)  I would have no problem with, say, requiring
parentheses in this one case; in all other cases, if you're expecting an
expression and you see an `if', you know what's up.  (Right?)

> And i'd probably think that
>     if if y<5: yep() else: 0:
> is the result of some editor-accident :-)

But that's true with any form of a conditional operator as the
conditional expression of an if statement (and quite frankly, in any
language that has both an if statement and a conditional operator);
that's just obtuse.  The only thing that makes it look like a stutter
here is the fact that you see two `ifs' in a row, which I think would be
almost a benefit for the jackass who wrote this code -- right away you
know someone did something weird instead of having to wade through it.

 Erik Max Francis / max at /
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ She glanced at her watch ... It was 9:23.
\__/ James Clavell Quake III Arena /
 A personal guide to Quake III Arena.

More information about the Python-list mailing list