Dangerous behavior of list(generator)
Martin v. Loewis
martin at v.loewis.de
Sat Jan 2 21:17:59 CET 2010
>> Bottom line, I'm going to have to remove this pattern from my code:
>> foo = (foo for foo in foos if foo.bar).next()
I recommend to rewrite this like so:
raise ValueError, "No first value"
foo = first(foo for foo in foos if foo.bar)
As others have said: don't let StopIteration appear unexpectedly;
IOW, consume generators right away in a loop construct (where
this first function is a loop construct as well). A different
way of writing it would be
for value in gen:
raise ValueError, "empty collection"
More information about the Python-list