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

Andrew Koenig ark at research.att.com
Mon Feb 10 00:50:23 EST 2003


Erik> I'm just saying saying that if you want a conditional expression
Erik> to be legal as a standalone statement, that requires a parser
Erik> change in how if statements are handled -- i.e., you have to
Erik> look ahead and see "oh, there's an `else:' on this same line, so
Erik> this is the conditional operator as a standalone statement, not
Erik> an if statement.  But ...

Right.  That's one reason I don't want a conditional expression to be
legal as a standalone statement.

>> If you parenthesize it:

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

>> then the "if" is no longer the first token in the statement, so it
>> introduces an if-expression.  No problem.

Erik> Right, and that was my suggestion somewhere in the thread (and
Erik> via email).  The corner case (which I shall hereafter refer to
Erik> as The Corner Case :-) -- where the conditional expression is
Erik> written as a standalone statement -- is not important to me, so
Erik> I have no problem disallowing it; that means no parser changes
Erik> are necessary (as far as I can tell) other than the introduction
Erik> of the conditional expression (which is an expression and thus
Erik> does not interfere with the if statement parsing.

I think we agree.

Erik> The only thing I might say about my disinterest in the Corner
Erik> Case is that it might be nice that if you think you're parsing
Erik> an if statement and see an `else:' on the same line, a slightly
Erik> more helpful SyntaxError is emitted (i.e., "Hey, you can't do
Erik> that; use parentheses or put the else on a separate line").  But
Erik> that's optional.

Yes.

Erik> I don't forsee myself using the Corner Case in real code, nor do
Erik> I see it becoming particularly popular, so I'm rather ambivalent
Erik> to its resolution.

Again, I think we agree.  I think the behavior should be whatever is
easiest to describe.

Erik> I must say I'm pretty convinced at this point that `if C: x
Erik> else: y' (and if we wish, its multiway variants `if C: x elif D:
Erik> y else: z') is absolutely the way to go if you want a
Erik> conditional operator.

I'd still be willing to live with `x if C else y', because I think
it's much better than `C and x or y'.




More information about the Python-list mailing list