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

Parzival Herzog parz at shaw.SpamBucket.ca
Mon Feb 10 22:15:38 CET 2003


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

>     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
BDFL,
notwithstanding BDFL's modest claim, that it is only 'fairly' Pythonic.
Fairly
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
                        "Unpythonic".

****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
or
    write."

> Alternatives
>

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

    (lambda: true_expression, lambda: false_expression)[not
test_expression]()

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
selection,
without the use of auxillary variables is already present in Python.

It follows that PEP 308 approval or disapproval must be ENTIRELY based on
the
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
true_expression
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
construction
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
needs
to be reversed, if it is to work correctly on a selection that /preceeds/
it,
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,
forsooth,
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
oriented
subversive cryptic Pythonistas can 'Use the Alternative, Luke!'

Long Live BDFL! (and PEP 308)

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







More information about the Python-list mailing list