[Python-ideas] Adding an optional function argument to all() and any() builtins

Andy Buckley andy at insectnation.org
Mon Nov 22 00:00:01 CET 2010

On 21/11/10 22:31, Ben Finney wrote:
> Masklinn <masklinn at masklinn.net> writes:
>> On 2010-11-21, at 21:11 , MRAB wrote:
>>> if any(mytestfunction(i) for i in mylist):
>>>    foo()
>> is not an improvement over `if any(map(mytestfunction, mylist))` or
>> `if any(imap(mytestfunction, mylist))`
> It is definitely an improvement by the criterion the OP mentioned, which
> was:
> Andy Buckley <andy at insectnation.org> writes:
>> I either have to explicitly re-code a slightly modified version of the
>> builtin, or wastefully use map() to apply my test to *all* the items
>> in the list.

Yes, at least syntactically: I had an odd blind spot here and never
considered using a comprehension/generator expression! Neat.

Still, Masklinn has suggested that this isn't necessarily performant
even in Py 2.7 or 3.x, so there still appears to be a use-case for an
extra argument to any/all. Or did I misunderstand the gencomp issue?

It might be nice to add a suggestion of gencomp use to the
documentation, as I'm sure there are others like me with (temporary)
comprehension blind spots who think those functions have more limited
applicability than is the case.


More information about the Python-ideas mailing list