question about generators

Neil Schemenauer nas at
Thu Aug 15 21:13:45 CEST 2002

Bernhard Herzog wrote:
> Andrew Koenig <ark at> 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 (


More information about the Python-list mailing list