1-line idiom to replace if blocks

Andrew Koenig ark at research.att.com
Wed Jan 22 14:39:03 CET 2003


>> And, of course, if you really want short-circuiting, you can write this:

>> result=[lambda:value-if-false, lambda:value-if-true][condition]()

Justin> I like it except I'd add "[not not condition]" to ensure a 0 or a 1.

You don't need to do that -- instead, you can write

   result=[lambda:value-if-true, lambda:value-if-false][not condition]()

-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark




More information about the Python-list mailing list