Coding style and else statements

Tal Einat tal.no.no.spam at gmail.com
Tue Aug 29 17:15:50 EDT 2006


Bruno Desthuilliers wrote:
> 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
>
Actually, the common work-around for this is:

(thing and [thing+1] or [-1])[0]

This works since non-empty lists are always considered true in
conditional context. This is more generic, and IMO more readable.




More information about the Python-list mailing list