Alan Hogan

Things Alan Hogan feels like sharing.

These are my comments on music, movies, books, web development and programming, Mac tips, and life in general. Enjoy!

Sat May 28

Simulating the Ternary Operator

x = (falseValue, trueValue)[condition]

That’s how people simulated the ternary operator in Python 2.4 and less. It’s interesting because it uses a fairly unique feature of Python — tuples — to make up for its lack of a language construct equivalent to common C-like-languages’ ternary operator (condition ? trueValue : falseValue).

Of course, now you should never write such obtuse code, given the actual Python ternary has existed for a while (trueValue if condition else falseValue).