[Python-Dev] Small any/all enhancement

Eric Nieuwland eric.nieuwland at xs4all.nl
Wed Dec 28 19:17:50 CET 2005


I wrote:
> all() can be terminated at the first false element. For very long
> sequences this has important performance benefits. Besides, it makes
> all(seq,pred) the equivalent of pred(seq[0]) and  pred(seq[1]) and
> pred(seq[2]) and ...

then, Martin v. Löwis wrote:
>
> And so does the version with generator expressions: Alex' expression
> will also terminate with the first false statement; it is equivalent
> to some_objects[0]==0 and some_objects[1]==0 and ...

and Alex Martelli wrote:
> 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.

Oops! Right you are. I was a bit too quick after seeing the use of 
map() proposed.

--eric



More information about the Python-Dev mailing list