[Python-Dev] Minimal 'stackless' PEP using generators?

Phillip J. Eby pje at telecommunity.com
Mon Aug 23 20:59:18 CEST 2004


At 02:33 PM 8/23/04 -0400, Clark C. Evans wrote:
>Perhaps it would be nice to add an alternative syntax to call a
>generator when you are expecting exactly one value.
>
>     def generator():
>         yield 'one value'
>     def consumer():
>          value = generator()

We have that today:

     value, = generator()

Or do I misunderstand you?


>Full-blown corountines arn't necessary.  A small tweak 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.

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.

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.  My comment about "coroutines" was more that Guido previously 
expressed distaste for adding such a communication mechanism to generators 
as abusing the concept of a generator just being a way to implement complex 
iterators.  Therefore, I thought a coroutine proposal (backed by a suitable 
syntax and an implementation plan) might have more success.



More information about the Python-Dev mailing list