Dangerous behavior of list(generator)

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Dec 31 06:44:29 EST 2009


On Wed, 30 Dec 2009 23:20:06 -0500, Benjamin Kaplan wrote:

>>> I used to have that a lot in cases where not finding at least one
>>> valid foo is an actual fatal error.
>>
>> What's wrong with the obvious solution?
>>
>> if not any(foo for foo in foos if foo.bar):
>>    raise ValueError('need at least one valid foo')
> 
> That would require 2 iterations through foos- once in the test, once for
> the assignment if successful. 

Remember though that any is a lazy test: it returns as soon as it gets a 
result. In the case of an empty list, it returns immediately with False, 
and in the case of a non-empty list, it returns immediately it reaches a 
true item. It doesn't matter if there are twenty thousand items, it will 
only look at the first so long as it is true.

Which of course answers my own question... what's wrong with using any is 
that it fails if the objects are all considered false in a boolean 
context, or if they might be. That means it will work for some objects 
(e.g. the re module's MatchObject instances which are always true), but 
not for arbitrary objects which may be false.



-- 
Steven



More information about the Python-list mailing list