Distinguishing active generators from exhausted ones
Ben Finney
ben+python at benfinney.id.au
Sat Jul 25 19:10:40 EDT 2009
Michal Kwiatkowski <constant.beta at gmail.com> writes:
> I may be missing something obvious here. Is there a better way to tell
> if a given generator object is still active or not?
foo = the_generator_object
try:
do_interesting_thing_that_needs(foo.next())
except StopIteration:
generator_is_exhausted()
In other words, don't LBYL, because it's EAFP. Whatever you need to do
that requires the next item from the generator, do that; you'll get a
specific exception if the generator is exhausted.
--
\ “Courteous and efficient self-service.” —café, southern France |
`\ |
_o__) |
Ben Finney
More information about the Python-list
mailing list