[Python-Dev] Re: PEP 255: Simple Generators

Neil Schemenauer nas@python.ca
Tue, 19 Jun 2001 07:00:39 -0700


Greg Ewing wrote:
> Something is bothering me about this. In fact,
> it's bothering me a LOT. In the following, will
> f() work as a generator-function:
> 
>   def f():
>     for i in range(5):
>       g(i)
> 
>   def g(i):
>     for j in range(10):
>       yield i,j
> 
> If I understand PEP255 correctly, this will *not*
> work.

No, it will not work.  The title of PEP 255 is "Simple
Generators".  What you want will require something like stackless
in order to get the C stack out of the way.  That's a major
change to the Python internals.  To make your example work you
need to do:

  def f():
    for i in range(5):
      for j in g(i):
        yield j

  def g(i):
    for j in range(10):
      yield i,j

Stackless may still be in Python's future but no for 2.2.

  Neil