filter() and booleanness

Michael Hudson mwh at python.net
Mon Jan 21 12:03:10 CET 2002


Peter Hansen <peter at engcorp.com> writes:

> 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.

There's also operator.truth().

It's all a little pointless, though.

Cheers,
M.

-- 
                    >> REVIEW OF THE YEAR, 2000 <<
                   It was shit. Give us another one.
                          -- NTK Know, 2000-12-29, http://www.ntk.net/



More information about the Python-list mailing list