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

Alan Daniels from_usenet at
Mon Feb 10 05:35:36 CET 2003

On Sun, 09 Feb 2003 16:26:06 -0800, Erik Max Francis <max at>

>The combination of the punctuation and a keyword still gives me an eek.

Actually, we have combinations of punctuation and keyword already,
because every "if" and "else" has to be followed by a colon character.
I agree that a ternary operator would be useful, and most people here
seem to want one, as long as it's "Pythonic", that is, easy to
understand at first glance, easy to read later on, and not prone to
abuse over time.

I'm proposing the...

  cond ? x else y

...syntax because I haven't really seen any more legible alternative.
I don't care for the original  proposal of "x if cond else y" because
to me, at least, it appears to get the cart before the horse. I'm just
used to thinking that the condition should come first, just like in
C's ternary operator, and just like the "if" blocks that Python
already has. Also, as we've seen from other examples, it doesn't nest
well. (At least Alex Martelli's proposal, to always put brackets
around the ternary, fixes the nesting part).

The only other proposed solution I've seen which I don't hate is to
have a builtin, something like "iff(cond, x, y)", where x and y are
deliberately not evaluated unless they're used. Not ideal, since it
introduces an exception to Python's typical evaluation rules, but at
least it's readable.

(P.S. I still don't see what's wrong with good ol' "cond ? x : y")

More information about the Python-list mailing list