[Python-ideas] generator.close

Nick Coghlan ncoghlan at gmail.com
Wed Apr 11 14:26:43 CEST 2012

On Wed, Apr 11, 2012 at 9:28 PM, Matt Joiner <anacrolix at gmail.com> wrote:
> You make an excellent point. I'm inclined to agree with you.

While Jacob does make a valid point about the question of what to do
when close() is called multiple times (or on a generator that has
already been exhausted through iteration), the specific reason that
got the idea killed in PEP 380 [1] is that generators shouldn't be
raising StopIteration as a result of a close() invocation anyway -
they should usually just reraise the GeneratorExit that gets thrown in
to finalise the generator body. If inner generators start making a
habit of converting GeneratorExit to StopIteration, then intervening
"yield from" operations may yield another value instead of terminating
the way they're supposed to in response to close().


[1] http://www.python.org/dev/peps/pep-0380/#rejected-ideas

Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list