conditional expressions

daniel w. moore dan65536 at hotmail.com
Wed Sep 18 22:20:03 EDT 2002


It's occurred to me after some brainstorming on the subject that the
C/perl construct

  a ? b : c

can be arranged like this in python:

  (a and [b] or [c])[0]

or, putting into lambda form, and using tuples for a slight efficiency
gain:

  cond_switch = lambda a,b,c : (a and (b,) or (c,))[0]

Has anyone put this to good use, i wonder?
Or have I just made a daring and heroic discovery in my own particular
idiom ;)
(http://www.stone-dead.asn.au/movies/holy-grail/scene-15.html)



More information about the Python-list mailing list