PEP 308 - suggestion for generalising the ternary operator

Erik Max Francis max at alcyone.com
Wed Feb 12 23:20:34 EST 2003


Damien Morton wrote:

> These "c and x or y" forms, while currently serving as the only way to
> implement the conditional selection operator, are far from intuitive.
> 
> The goal of the PEP 308 discussion is to come up with a syntax for
> conditional selection which is clear, consise, and intuitive. I dont
> think
> obscure Pythonic workarounds are a good form to start from.

Well said.  The and/or hack is just that:  a hack that takes advantage
of the semantics of Python's logical operators.  We all know that it's
also a broken hack (in that it doesn't work for some values of its
operands), but even if it _did_ work it is still highly idiomatic --
upon encountering it, it's quite impenetrable.  To this day, I only
recognize it because it's been discussed to death in these conditional
expression threads; otherwise, it would still be as unreadable to me
today as the first time I saw it mentioned (after which I immediately
forgot it).

The goal here is to get a _true_ conditional operator (that inherently
means one with short-circuiting) that is at least somewhat readable and
intuitive.  Contrary to what a lot of anti-PEP people have been
spouting, the pro-PEP people (I will wager, I cannot speak for all of
them) are _not_ after a conditional operator _no matter what_.  They're
after a conditional operator that's readable, and they're sure that
something will fit that bill.

Building on the highly idiomatic and/or form is a fundamentally flawed
approach, since you're starting from unreadable (and broken) idiom and
trying to correct that brokenness, rather than addressing the fact that
what you'll come up with (whether it's `C and x else y' or `C and {x}
else y' is still unreadable.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ I always entertain great hopes.
\__/ Robert Frost
    Bosskey.net / http://www.bosskey.net/
 A personal guide to online multiplayer first person shooters.




More information about the Python-list mailing list