[Python-ideas] Allow filter(items)

Joshua Landau joshua at landau.ws
Wed Aug 7 04:46:53 CEST 2013


On 5 August 2013 08:46, Peter Otten <__peter__ at web.de> wrote:
> filter(items)
>
> looks much cleaner than
>
> filter(None, items)
>
> and is easy to understand. 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
>
> but this is a noisy error. I think the advantage of making the magic None
> redundant outweighs this potential pitfall.


I'll put forward my side on the issue. I'm for "filter(items)" just
because I'd never use "filter(lambda i: ..., items)" over "(i for i in
items if ...)". To me the filter(items) form is the only form that
actually makes sense for me in the majority of cases as I rarely have
the key function predefined.

Additionally, keyword arguments can't solve this completely as
"filter(iterable, key=pred)" would raise an error just like:

    >>> (lambda x=0, y=0: ...)(0, x=0)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: <lambda>() got multiple values for argument 'x'

unless we break backward compatibility or make some very odd inconsistencies.

That said, there's something to letting keyword arguments reorder
function calls -- this isn't the only time I've wanted to do it. It'd
make most sense if more builtins took keyword arguments though.


More information about the Python-ideas mailing list