question about generators

Neil Schemenauer nas at python.ca
Thu Aug 15 15:13:45 EDT 2002


Bernhard Herzog wrote:
> Andrew Koenig <ark at research.att.com> writes:
> > The "yield 1" statement does two things:
> > 
> >         1) It creates a generator object and returns that object to
> >            the caller;
> 
> It doesn't. This happens when f is called. However, f doesn't really
> start to execute until the generator's next() method is called

Right.  Whether 'def something' is going to be a "generator function" or
a plain function is determined at compile time by examining the body for
yield statements.  When a generator function is called it returns a
generator-iterator.  Calling .next() on the generator-iterator begins
execution of the code.

For all the details refer to PEP 255 (www.python.org/peps/pep-0255.html).

  Neil




More information about the Python-list mailing list