[Python-ideas] Allow filter(items)

David Mertz mertz at gnosis.cx
Mon Aug 5 18:56:58 CEST 2013


On Mon, Aug 5, 2013 at 12:46 AM, Peter Otten <__peter__ at web.de> wrote:
> filter(None, items)
> filter(bool, items)
> filter(len, items)
> filter(lambda s: s != "", strings)

These are NOT "alternatives" ... they mean very different things (but
will in some cases produce the same result.  Well, except that 'None'
and 'bool' are equivalent above.

-0.5 so far on this.  Since a one-argument version of filter() is now
simply an error, this change won't break any existing code.  However,
I feel like it will invite future errors: having to be explicit about
what predicate you are filtering by--even if it is bool()--keeps users
honest in actually stating what the predicate is rather than relying
on "implicit identity function".

> filter(items, predicate) # wrong
> but this is a noisy error. I think the advantage of making the magic None
> redundant outweighs this potential pitfall.

This may not be a noisy error, nor any error at all that Python can
detect.  It is perfectly possible for a Python object to have both a
.__call__() and a .__iter__() method.  It can well be the case that
only the *programmer* and not the language needs to decide which thing
is the iterator and which one predicate.

Yours, David...

-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.


More information about the Python-ideas mailing list