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

Parzival Herzog parz at
Mon Feb 10 22:15:38 CET 2003

"Guido van Rossum" <guido at> wrote in message
news:mailman.1044638179.4983.python-list at

>     Requests for an if-then-else ("ternary") expression keep coming up
>     on comp.lang.python.  This PEP contains a concrete proposal of a
>     fairly Pythonic syntax.
> Proposal
>     The proposed syntax is as follows:
>         <expression1> if <condition> else <expression2>

I vote to accept this proposal.

PEP 308:  +1e308

The proposed syntax is, by definition, 'Pythonic', being authored by the
notwithstanding BDFL's modest claim, that it is only 'fairly' Pythonic.
Pythonic IS Pythonic.

*** I challenge anyone to come up with an empirically verifiable and
universally applicable definition of 'Pythonic feature', other that
'Pythonic' is whatever BDFL says is 'Pythonic'.

****Corollory: No-one but BDFL can declare something to be "Pythonic" or

****Corollary 2: "Pythonic" does not mean "what I am used to reading", or
    "what I would like to read/write", or "what I would like others to read

> Alternatives

The alternative to PEP 308 is already available in Python. Here is the

    (lambda: true_expression, lambda: false_expression)[not

for example:

>>> (lambda: 'nice! '*5, lambda: 'naughty! '*3)[not 2*2 == 4]()
'nice! nice! nice! nice! nice! '

So, everyone must accept that the semantics of lazily evaluated conditional
without the use of auxillary variables is already present in Python.

It follows that PEP 308 approval or disapproval must be ENTIRELY based on
merits or demerits of the syntax.

Notice that the 'not' is necessary, to ensure that an arbitrary
'test_expression' provides
a 'boolean' (0 or 1) result, with the pleasing side-effect of ordering the
first. I really like the '()' at the end, which is like a little pat on the
back, a sort of
'Jolly good coding, my fine fellow!' Lots of 'lambda's make this
a standout, and the colons can help ease the readability for the
traditionalists, who
had the colon to hold onto with their old familiar 'if: ... else: ... ' .
The 'not' is
also a good reminder that the test expression is at the end, and therefore
to be reversed, if it is to work correctly on a selection that /preceeds/
as opposed to one which /follows/ it, as in the more familiar 'if test: ...
And there is a nice symmetry with the two pairs of  parentheses ( ... )
[...] ()
surrounding the bracketted inverted test.

Oh, but I argue for the 'Alternative', which is already in Python, when,
I truly wish to plead the case for the 'Proposal,' which is not.

P.S. Should PEP 308 be rejected, all you sneaky functional expression
subversive cryptic Pythonistas can 'Use the Alternative, Luke!'

Long Live BDFL! (and PEP 308)

Yours Alternative-ly:
(reply-to is munged for spammers)

More information about the Python-list mailing list