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

Greg Ewing greg@cosc.canterbury.ac.nz
Wed, 20 Jun 2001 18:28:13 +1200

Olaf Delgado Friedrichs wrote:
> If I understand correctly, this should work:
>  def f():
>    for i in range(5):
>      for x in g(i):
>        yield x
>  def g(i):
>    for j in range(10):
>      yield i,j

Yes, I realised that shortly afterwards. But I think
we're going to get a lot of questions from newcomers
who have tried to implicitly nest iterators and are
very confused about why it doesn't work and what needs
to be done to make it work.

An explicit generator definition syntax would help
here, I think. First of all, it would be a syntax
error to use "yield" outside of a generator definition,
so they would be forced to declare the inner one
as a generator. Then, if they neglected to make the
outer one a generator too, it would look like this:

  def f():
    for i in range(5):

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

from which it is glaringly obvious that f()
is NOT a generator, and therefore can't be
used as one.

Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page: