[Python-ideas] Allow filter(items)

Terry Reedy tjreedy at udel.edu
Mon Aug 5 22:36:39 CEST 2013


On 8/5/2013 3:46 AM, Peter Otten wrote:
> filter(items)
>
> looks much cleaner than
>
> filter(None, items)
>
> and is easy to understand.

True, if you call the iterable 'items', but filter(abc) is less obvious. 
Is 'abc' an iterable to be filtered, or a predicate missing the iterable 
to filter?

Optional leading args cannot be directly expressed as a Python 
signature. They are a nuisance to document as well as to simulate. I 
consider range, the only example I can think of at the moment, as 
something *not* to be imitated. Range gets away with the oddity because 
start and stop must both be ints. For filter, 'predicate' and 'iterable' 
are neither identical nor disjoint.

As for the last point, an object can have both .__iter__ and .__call__. 
For example, a collection with __contains__ could have
     __call__ = __contains__
Then 'instance(x)' is the same as 'x in instance', except that the 
instance is a callable predicate that can be passed to functions like 
filter, whereas the expression 'in instance' cannot be.

> Fewer people would use alternative spellings like
>
> filter(bool, items)
> filter(len, items)
> filter(lambda s: s != "", strings)
>
> The signature change may lead you to spell
> filter(predicate, items) # correct
> as
> filter(items, predicate) # wrong

I think it easy to guess that it will confuse people.

> but this is a noisy error.\

Maybe, but not guaranteed to be. See above.

> I think the advantage of making the magic None
> redundant outweighs this potential pitfall.

I think the opposite.
-1

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list