[Python-ideas] Revised revised revised PEP on yield-from

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Feb 17 06:37:01 CET 2009

Raymond Hettinger wrote:

> Looks like a language construct where only a handful
> of python programmers will be able to correctly describe
> what it does.

The whole area of generators is one where I think only
a minority of programmers will ever fully understand
all the gory details.

Those paragraphs are there for the purpose of providing
a complete and rigorous specification of what's being
proposed. For most people, almost all the essential
information is summed up in this one sentence near the

   The effect is to run the iterator to exhaustion,
   during which time it behaves as though it were
   communicating directly with the caller of the
   generator containing the ``yield from`` expression.

As I've said, I believe it's actually quite simple
conceptually. It's just that it gets messy trying to
explain it by way of expansion into currently existing
Python code and concepts.

> This seems like it is awkwardly trying to cater to two competing needs.
> It recognized that the outer generator make have a legitimate need
> to catch an exception and that the inner generator might want it too.
> Unfortunately, only one can be caught and there is no way to have
> both the inner and outer generator/iterator each do their part in
> servicing an exception.

I don't understand what you mean by that. If you were
making an ordinary function call, you'd expect that the
called function would get first try at catching any
exception occurring while it's running, and if it doesn't,
it propagates out to the calling function.

Also it's not true that only one of them can catch the
exception. The inner one might catch it, do some processing
and then re-raise it. Or it might do something in a finally

My intent is for all these things to work the same way
when one generator delegates to another using yield-from.


More information about the Python-ideas mailing list