filter() and booleanness
Peter Hansen
peter at engcorp.com
Sun Jan 20 17:26:05 EST 2002
rihad wrote:
>
> Hi there, the following two calls will both print ('hi', 'there') due
> to None being substituted for `identity function' (as the docs say):
>
> print filter(None, ('hi', None, 0, 'there'))
> print filter(lambda x: x, ('hi', None, 0, 'there'))
>
> Then I thought I'd clamp the x to [0..1] but things like x != 0 missed
> None and x != None missed 0. :) Now I have lambda x: not not x, is it
> bulletproof? (it seems to work).
I believe I understand what you're asking :-), and yes,
it _is_ bulletproof. That's an idiom for mapping false
to 0 and true to 1, regardless of the original values.
More information about the Python-list
mailing list