A library approach to the ternary operator
David Abrahams
dave at boost-consulting.com
Fri Mar 21 16:40:16 CET 2003
I still like the idea of a ternary operator, and I'm not afraid to
admit it. However, none of the language solutions were particularly
compelling, so I got to thinking about a library approach and came up
with this one:
---- ternary.py-------
def select(x): return x[0]
---------------------
# then...
from ternary import *
select(condition and [true_result] or [false_result])
it's just enough prettier than
(condition and [true_result] or [false_result])[0]
to be worth using. Having a function name ("select") there gives us
something to attach a nice verbose comment to, while the above, since
it uses only language primitives, remains obscure.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Python-list
mailing list