[Python-ideas] Allow filter(items)

Shane Green shane at umbrellacode.com
Mon Aug 5 19:22:08 CEST 2013


However: 

filter(None, items) and 
filter(lambda item: item, items) 

are alternatives, correct?  If so, then was it also correct to say that None as the first parameter is the same as the identity function?  

If those things are true, then I still think passing the iterator itself as the first argument is actually more clear and accurate than the current approach; either that, or an explicit identity function, as None does not have a __call__ that acts as an identity function or boolean evaluation so it’s use here doesn’t make much sense to me; it’s just the way it’s always worked.  


On Aug 5, 2013, at 9:56 AM, David Mertz <mertz at gnosis.cx> wrote:

> 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.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130805/64ed5a46/attachment.html>


More information about the Python-ideas mailing list