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

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Feb 17 07:31:42 CET 2009

Guido van Rossum wrote:

> I don't quite
> understand how I would write the function that is delegated to as
> "yield from g(x)" nor do I quite see what the caller of the outer
> generator should expect from successive next() or .send() calls.

It should be able to expect whatever would happen if the
body of the delegated-to generator were inlined into the
delegating generator.

That's the core idea behind all of this -- being able to
take a chunk of code containing yields, abstract it out
and put it in another function, without the ouside world
being any the wiser.

We do this all the time with ordinary functions and
don't ever question the utility of being able to do so.
I'm at a bit of a loss to understand why people can't
see the utility in being able to do the same thing
with generator code.

I take your point about needing a better generators-
as-threads example, though, and I'll see if I can come
up with something.

> So, "return" is equivalent to "raise StopIteration" and "return
> <value>" is equivalent to "raise StopIteration(<value>)"?



More information about the Python-ideas mailing list