[Python-ideas] Allow filter(items)

Shane Green shane at umbrellacode.com
Mon Aug 5 21:11:51 CEST 2013


+1 (still) for the additional reason that it just makes sense: 

filter(bool, items) => “Filter this list of items using predicate.” 
filter(items) => “Filter this list of items.” 

To me it makes perfect sense that the second, shorthand form would mean filter falsy items from the list; it’s precisely what I would expect a function named “filter” to do to a list of items in the absence of other arguments.  

Finally, while it’s true “bool” is explicit but redundant when you consider None’s meaning in this context to be “bool”, if you consider its meaning to be the identity function instead–which is the interpretation I tend to think of because filter, if I’m not mistaken, predates True/False and __bool__, etc.–then it can be argued that the use of “bool” is superfluous rather than explicit/redundant. 




On Aug 5, 2013, at 11:31 AM, João Bernardo <jbvsmo at gmail.com> wrote:

> -1 There should be only one way and there are lots of ways already. Use a genexp for readability: (x for x in items if x)
> 
> João Bernardo
> 
> 
> 2013/8/5 Serhiy Storchaka <storchaka at gmail.com>
> 05.08.13 20:32, Markus Unterwaditzer написав(ла):
> 
> -0, for reasons already mentioned. While i agree that filter(None, items) is counterintuitive, filter(bool, items) looks very readable to me.
> 
> filter(bool, items) is redundant in same sense as `if bool(x)`, `for x in iter(items)`, or `"%s" % str(x)`. A result of the predicate is implicitly called to boolean.
> 
> 
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
> 
> _______________________________________________
> 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/7f8f3e03/attachment.html>


More information about the Python-ideas mailing list