short-circuiting any/all ?

Steven D'Aprano steven at
Tue Mar 23 02:30:37 CET 2010

On Mon, 22 Mar 2010 22:19:57 +0000, kj wrote:

> In <mailman.1069.1269283393.23598.python-list at> Tim Golden
> <mail at> writes:
>>On 22/03/2010 18:30, kj wrote:
>>> Thanks!  I'm glad to know that one can get the short circuiting using
>>> a map-type idiom.  (I prefer map over comprehensions when I don't need
>>> to define a function just for the purpose of passing it to it.)
>>In what way does "map" over "comprehensions" save you defining a
>>any (map (is_invalid, L))
>>any (is_invalid (i) for i in L)
> I was talking in the *general* case.  map at the very least requires a
> lambda expression, which is a one-time function defintion.

But keep in mind that instead of this:

map(lambda x,y: x+y, somelist)

you can do this:

import operator
map(operator.add, somelist)

In any case, the once-off cost of creating or importing a function is 
usually quite cheap. As usual, the best advise is not to worry about 
optimization until you have profiled the code and learned where the 
actual bottlenecks are. Write what reads best, not what you guess might 
be faster, until you really know you need the speed and that it is an 
optimization and not a pessimation.


More information about the Python-list mailing list