[Python-Dev] Small any/all enhancement

Alex Martelli aleaxit at gmail.com
Wed Dec 28 16:55:13 CET 2005


On Dec 27, 2005, at 11:06 PM, Eric Nieuwland wrote:
    ...
>>> def zerop(x):
>>>     return x==0
>>>
>>> all(some_objects, zerop)
>>
>> and why would that be better than
>> all(o==0 for o in some_objects)
>> ?
>
> all() can be terminated at the first false element. For very long
> sequences this has important performance benefits. Besides, it makes

Of course it can -- in both formulations.  genexp's are also computed  
"as needed", only one item at a time: you appear to imply they don't,  
maybe you're confusing them with list comprehensions.  What I'm  
asking is, what are the ADVANTAGES of the pred form, that make it  
worth paying the conceptual cost of having "two obvious ways" to do  
one task.

> all(seq,pred) the equivalent of pred(seq[0]) and  pred(seq[1]) and
> pred(seq[2]) and ...

...and also the equivalent of all(pred(s) for s in seq) -- which is  
exactly my problem: I don't like redundant good ways of expressing  
identical tasks.  The genexp will often be more compact, whenever the  
'pred' requires a def, a lambda, or something like  
operator.attrgetter, anyway.

Alex




More information about the Python-Dev mailing list