February 20, 2003
PEP 308 - run away, run away!

Over the last couple of weeks there has been the Mother and Father of all debates (degenerating into ranting, trolling, and personal abuse at times) on c.l.py regarding the possible introduction of a ternary operator to Python. Guido is happy to add one, or not, but he wants the community's consensus of opinion for once and for all - should there be a ternary operator in Python, and if so, what should its syntax be?

I've not really used anything like the ternary operator. Java has one, but I don't use it, and my cow orkers don't know what it is, so they don't either. So, I'd be happier doing without it.

But if we must have one, then I'd prefer one of the more Pythonic syntaxes. Let's use words, not punctuation! Of all of them, I dislike:

result = (if condition then expression1 else expression2)

least. It has a slightly list comprehension like feel to it, and I think that its meaning would be fairly obvious even to someone who had never even heard of the construct, which is a sign of good syntax, I think. But I gather that there is some problem parsing this. Ho hum...

Update: Turns out that Mark does know what the ternary operator is, and uses it, but didn't know it by that name. Sigh.

Update: Guido has spoken. There will be no ternary operator in Python.

Posted to Python by Simon Brunning at February 20, 2003 02:36 PM
Post a comment

Email Address:



Remember info?