PEP 308: Alternative conditional operator forms

Erik Max Francis max at
Mon Feb 10 08:54:55 CET 2003

David Gausebeck wrote:

> Or at least I believe that's the case... there's some discussion going
> on about that right now in the main PEP thread.  If it turns out that
> the interpreter can nicely support
> lval = if C: x else: y
> then I'd be perfectly happy with it.  My support for an alternate
> keyword is based on the assumption that the above syntax can't work.

I don't see why it can't.  As far as I can tell (someone please correct
me if this is wrong), the `if' that starts an if statement can always be
distinguished from the `if' that starts the conditional operator except
in one case (The Corner Case), where a conditional operator is a
solitary expression as a lone statement.  That case doesn't interest me,
so it would be perfectly satisfactory to me for Python to interpret that
as an error, since it's better written as an actual if statement anyway.
(Parenthesize it or transform it to a real, live if statement; your

I can't see any other conflicts than that, because in a script when the
parser is doing its thing, it knows whether it's expecting a statement
or an expression.

The only other case I can think of, which someone offhandedly alluded to
but didn't go into depth, would be the "single" compilation, as the
interactive interpreter does, where it will take a string and interpret
it either as an expression or a statement depending on which it is.  In
this case it would be perfectly satisfactory to me for it to default to
the if statement, not the conditional operator.  It's just a matter of
choosing which you'd prefer.  I just see that as really another example
of the Corner Case; it's a who-cares-as-long-as-it's-consistent

> hmm... looking at that, it seems that the interpreter would have
> enough context by the time it gets to the 'if' to know if it needs to
> be an expression or not.  If the 'if' is an rvalue (function arg, list
> element, etc.) then it's an if-expression, otherwise it's a normal
> 'if'.

Right.  If the `if' is the first keyword in a statement or single, then
it's the if statement.  If it's the first keyword in an expression, then
it's the conditional operator.  I see no conflicts.

Again, please someone correct me if I'm wrong here.

 Erik Max Francis / max at /
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ No man quite believes in any other man.
\__/ H.L. Mencken
    Alcyone Systems /
 Alcyone Systems, San Jose, California.

More information about the Python-list mailing list