[Python-ideas] Allow filter(items)
Shane Green
shane at umbrellacode.com
Mon Aug 5 19:51:19 CEST 2013
Yes, but omitting an identity function and just passing its input–an iterable in this case–seems like a reasonable “shortcut” in general. And wouldn’t it be relatively easy to make the None parameter passed to filter for this usage optional, without introducing any compatibility issues?
On Aug 5, 2013, at 10:31 AM, David Mertz <mertz at gnosis.cx> wrote:
> On Mon, Aug 5, 2013 at 10:22 AM, Shane Green <shane at umbrellacode.com> wrote:
>> 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?
>
> Of course. The above are equivalent, and both are also equivalent to
> the only form *I* would ever think of using: filter(bool, items).
> Doing a bool(...) is always implied in evaluation--or maybe more
> accurately, applying it is idempotent.
>
> I agree that 'None' as a placeholder predicate is strange and
> unintuitive. I would probably be at least +0.5 on deprecating that,
> and requiring predicates be actual callables. However, that change
> *would* break some existing code.
>
>> 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.
>
> If we could go back in time and reverse the order of 'pred' and 'iter'
> in the API of filter(), I'd probably support that. However, every
> single Python program that uses filter() now assumes the current
> argument order. The breakage there is far too great to consider now.
>
>
> --
> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130805/f9a313e2/attachment.html>
More information about the Python-ideas
mailing list