[Python-Dev] Adding C ternary select (a?b:c) to Python?

Tim Peters tim_one@email.msn.com
Sun, 30 Jan 2000 18:25:30 -0500


[Guido van Rossum]
> Eric Raymond has sent me the following patch, which adds conditional
> expressions to Python.  I'd like to hear opinions on whether this
> is a good thing to add to Python,

Marginal; not evil, but of minor utility.

> and whether this is the right syntax.

If and only if you were a C programmer first.  BTW, insert <wink>s where
needed throughout <wink>.

> I am a bit skeptical about whether this is sufficiently Pythonic,
> but on the other hand there have always been requests for such a
> feature,

There have always been requests for the union of all features from all other
languages.

> and the existing solutions are ugly:
>
>   a and b or c
>
> only works when you know for sure that b will never be false,

Too error prone.

> and
>
>   (a and [b] or [c])[0]
>
> is dead ugly...

Well, I'm the guy who invented that one!  The thread that spawned it was
just playfully wondering whether it was *possible* -- and if I didn't solve
it, Majewski would have using even uglier __xxx__ trickery.  I've never used
it in a real program, and shoot people who do.

So there is no reasonable way to spell ?: as a one-liner today, period.  The
question is whether that's "a lack" worth doing something about.  I can live
without it.

Surprised that Jim (Fulton) seems so keen on it:  his cPickle.c uses ?:
exactly once in 4400 lines of C, and in a line that would have been clearer
if C had a max function (or is it min?  it can take a while to
reverse-engineer the intent of a ?:! ... not good when it happens; and I
recall one of the contributed patches that went into 1.5.2 longobject.c,
that had Guido & I both cracking a C manual just to figure out how C would
*parse* a particularly nutso blob of nested ?: thingies).

If this goes in (I'm not deadly opposed, just more opposed than in favor),
I'd like to see "else" used instead of the colon (cond "?" true "else"
false).  The question mark is reasonably mnemonic, but a colon makes no
sense here.

Now let's see whether people really want the functionality or are just
addicted to C syntax <ahem>.

BTW, a number of other changes would be needed to the Lang Ref manual (e.g.,
section 2.6 (Delimeters) explicitly says that "?" isn't used in Python
today, and that its appeareance outside a string literal or comment is "an
unconditional error"; etc).

crabby-old-man-ly y'rs  - tim