[Python-ideas] Yield-From: GeneratorExit?

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Mar 22 10:26:08 CET 2009

I'm having trouble making up my mind how GeneratorExit
should be handled.

My feeling is that GeneratorExit is a peculiarity of
generators that other kinds of iterators shouldn't have
to know about. So, if you close() a generator, that
shouldn't imply throwing GeneratorExit into the
subiterator -- rather, the subiterator should simply
be dropped and then the delegating generator finalized
as usual.

If the subiterator happens to be another generator,
dropping the last reference to it will cause it to
be closed, in which case it will raise its own
GeneratorExit. Other kinds of iterators can finalize
themselves however they see fit, and don't need to
pretend they're generators and understand

For consistency, this implies that a GeneratorExit
explicitly thrown in using throw() shouldn't be
forwarded to the subiterator either, even if it has
a throw() method.

To do otherwise would require making a distinction that
can't be expressed in the Python expansion. Also, it
seems elegant to preserve the property that if g is a
generator then g.close() and g.throw(GeneratorExit) are
exactly equivalent.

What do people think about this?


More information about the Python-ideas mailing list