[Python-Dev] Minimal 'stackless' PEP using generators?
Clark C. Evans
cce at clarkevans.com
Mon Aug 23 21:18:49 CEST 2004
On Mon, Aug 23, 2004 at 02:59:18PM -0400, Phillip J. Eby wrote:
| > def generator():
| > yield 'one value'
| value, = generator()
Oh, cool. ;)
| > Full-blown corountines arn't necessary. A small tweak to
| > to generators will do.
| I don't think this is true. Your hypothetical example can't resume
| 'top()' after it yields the "co-operate" control value, unless it either
| it *has* a stack of generators, or the Python core somehow maintains a
| stack of the executing generators.
Yes. The idea is for Python to maintain a stack of generators, and when
ever the top-most generator yields <magic> this yield skips the
intermediate generators on the stack, and goes immediately to the
| So, my point was that since this can already be done in user-level code
| with a stack of generators, I don't see the point to adding a facility to
| Python to create hidden stacks of generators.
The problem is, if you maintain the stack of generators in userland,
then each generator on the stack must do special stuff to handle
'cooperate' (in one form or another). You demonstrated an equivalent
problem with your peek.event code presented earlier. While, if this is
built into the core, intermediate generators can be happily oblivious to
the cooperative magic.
| Instead, it would be more useful to get rid of the one piece that really
| is "magic", by providing a way to pass values or exceptions into a
| running generator.
I don't like this either.
More information about the Python-Dev