Coding style and else statements

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon Aug 28 19:16:21 EDT 2006


Sam Pointon a écrit :
> Bruno Desthuilliers wrote:
> 
>>foo = lambda thing: thing and thing + 1 or -1
> 
> 
> The and ... or trick is buggy (what if thing == -1?)

Yes, true - Should be:
foo2 = lambda t: t != -1 and (t and t+1 or -1) or 0


> and bad style. 

Lol. Well, so what about:
foo = lambda t: (lambda t: -1, lambda t: t+1)[bool(t)](t)

?-)

(NB: don't bother answering)


> If
> you -do- want a conditional expression, 2.5 provides one:

Yes, at last, and I'm glad it finally made it into the language. But 2.5 
is not here yet. The 'and/or' trick can be, well, tricky, (notably in 
this case) but it at least work with most Python versions.


> No subtle logical bugs, and a good deal more obvious.

Indeed.



More information about the Python-list mailing list