[Python-ideas] Yield-From Example 2: Scheduler

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Feb 20 02:04:48 CET 2009

Adam Olsen wrote:

> You don't use plain "yield" except internally, so you don't even need
> "yield Call(g(x))".  You only need "yield g(x)" and your scheduler can
> assume it's a generator.

I was about to answer that you need some way to distinguish
between calls and returns, but if we have StopIteration(value)
then that's not true. So you're right, this is a viable way
of implementing generator scheduling.

So there perhaps isn't a very strong case for using 'yield
from' in a scheduler setting. However it still illustrates
the utility of being able to return a value from a generator,
since otherwise you would either have to explicitly raise
the StopIteration or resort to some convention such as
yield Call(g)/yield Return(x).

Also there may be a performance advantage, since there
will be some cost involved in using Python code to manage
the generator stack and route returned values to the right

In a wider setting, I still think there is benefit in
being able to abstract out pieces of generator code
without needing to have a special driver.


More information about the Python-ideas mailing list