>> 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:
def first(gen):
try:
return gen.next()
except StopIteration:
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
def first(gen):
for value in gen:
return value
raise ValueError, "empty collection"
Regards,
Martin
--
http://mail.python.org/mailman/listinfo/python-list