[Python-ideas] Yield-From: Handling of GeneratorExit

Guido van Rossum guido at python.org
Thu Apr 2 19:44:36 CEST 2009


On Thu, Apr 2, 2009 at 4:28 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> I've had another idea about this. Suppose the close()
> method of a generator didn't complain about reaching
> a yield after GeneratorExit is raised, but simply
> raised it again, and continued doing so until either
> a return occured or an exception propagated out.
>
> Seems to me this couldn't do any harm to a well-
> behaved generator, since it has to be prepared to
> deal with a GeneratorExit arising from any of its
> yield points.

The feature doesn't exist for the benefit of well-behaved generators.
It exists to help people who don't understand generators well enough
yet to only write well-behaved ones. This is an important goal to me
-- generators are a complex enough topic that I prefer to be on the
strict side rather than giving weird code a random meaning.

> Yield-from would then no longer have the potential
> to create broken generators, we wouldn't have to treat
> GeneratorExit differently from any other exception,
> and Jacob could have his subgenerators that return
> values when you close them.

I need a longer description of the problems that you are trying to
solve here -- I haven't been able to catch up with all the threads.
How would yield-from create a broken generator? (As opposed to all the
ways that allowing GeneratorExit to be ignored allows creating broken
generators.) Is there an example shorter than a page that shows the
usefulness of subgenerators returning values when closed?

Please, please, please, we need to stop the bikeshedding and scope
expansion, and start converging to a *simpler* proposal.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-ideas mailing list