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

Chris Rebert pyideas at rebertia.com
Sun Nov 21 23:03:07 CET 2010


On Sun, Nov 21, 2010 at 1:35 PM, Masklinn <masklinn at masklinn.net> wrote:
> On 2010-11-21, at 21:43 , Alexander Belopolsky wrote:
>> On Sun, Nov 21, 2010 at 3:20 PM, Masklinn <masklinn at masklinn.net> wrote:
>>> 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))`
>>
>> In 2.7, both MRAB's generator expression solution and your imap() are
>> improvements over map() because they don't create a temporary list.
>
> Which doesn't actually mean it's an improvement, gencomps are not always faster than map when the transformer is a function in all cases.

In many cases it will be though, because the gencomp is not eagerly
evaluated, unlike listcomps and old map()s. gencomps are also
definitely more memory efficient.

Cheers,
Chris



More information about the Python-ideas mailing list