[Python-ideas] Allow filter(items)

Stephen J. Turnbull stephen at xemacs.org
Thu Aug 8 03:12:12 CEST 2013


Shane Green writes:

 > I wanted to add that my answer is basically geared around answering
 > the question, okay, if you had to define this with a predicate, what
 > would the predicate be?  I’m not literally saying the identity
 > function is used.

But then, because of the way Python treats expressions in Boolean
contexts, you cannot distinguish any of the three treatments "use the
iterable element as-is" (how None is actually implemented), "apply the
identity function lambda x: x", and "apply bool", because all of them
use the result of applying bool to the iterable element:

    bool(x) == bool((lambda z: z)(x)) == bool(bool(x)) for all x

where the outer bool is probably inlined in the interpreter rather
than an actual application of the function bool.


More information about the Python-ideas mailing list