(test) ? a:b

Michael Torrie torriem at gmail.com
Wed Oct 22 19:16:48 CEST 2014

On 10/22/2014 05:45 AM, Mark Lawrence wrote:
>>> without not:
>>> j = [j+1, 3][j>=10]
>>> with not:
>>> j = [3, j+1][not (j>=10)]
>> Oh it's a trick !
>> thx
> IMHO it's just dreadful.  Why people insist on messing around like this 
> I really don't know, it just drives me nuts.

This actually was the standard idiom used by many python programs before
Python 2.5.  But I agree.  Don't do this anymore! Python has a ternary
if expression. Also the ternary if expression does, I believe
short-circuit logic, so the non-chosen path is not calculated.  This
hack does not. Could lead to interesting bugs depending on your assumptions.

More information about the Python-list mailing list