[Python-Dev] PEP 380 (yield from a subgenerator) comments

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Mar 22 09:11:29 CET 2009


P.J. Eby wrote:

> (I'm thus finding it hard 
> to believe there's a non-contrived example that's not doing I/O, 
> scheduling, or some other form of co-operative multitasking.)

Have you seen my xml parser example?

http://www.cosc.canterbury.ac.nz/greg.ewing/python/yield-from/

Whether you'll consider it contrived or not I don't know
(contrivedness being such a subjective property) but it
illustrates the style of programming I'm trying to support
with the return-value feature.

> In any case, you didn't address the confusion issue: the inability of 
> generators to return a value is there for a good reason,

It's there because formerly there was nowhere for the
return value to go. If there is somewhere for it to go,
the restriction will no longer be needed.

Things like this have happened before. It used to be
forbidden to put a yield in a try-finally block, because
there was no way to ensure that the finally would be
executed. Once a way was found to do that, the restriction
was lifted.

As for confusion, we ignore the return values of function
calls all the time, without worrying that someone might be
confused by the fact that their return value doesn't go
anywhere. And that's the right way to think of a yield-from
expression -- as a kind of function call, not a kind of yield.

If there's anything confusing, it's the presence of the
word 'yield'. Its only virtue is that it gives a clue that
the construct has something to do with generators, but
you'll have to RTM to find out exactly what. Nobody has
thus far suggested any better name, however.

-- 
Greg


More information about the Python-Dev mailing list