Generators and exceptions (was Re: Stackless Python and Python 2.x)

Aahz Maruch aahz at
Fri Sep 7 06:30:14 CEST 2001

In article <mailman.999833409.3011.python-list at>,
Tim Peters < at> wrote:
>a is resumed, it executes the "yield 1" and suspends, returning 1 as the
>value of the "" expression.  Note that the b function has nothing to
>do with this:  a generator always returns to its immediate *invoker*, not
>necessarily to the function that created the iterator.  After "g = b()", b's
>role in this is finished.

Gotcha.  So after the generator gets suspended, the call chain in using
generators always refers to the frame in which executes.  I
think adding that statement to the PEP would be helpful in reducing

To expand on this a bit, essentially a generator lives in two contexts:

* In the first context, a generator-iterator is created, but no code
within the generator is executed.  This means that it is functionally
impossible to raise an exception at this point (aside from the usual
NameError if the generator object isn't found).  The generator is
created in a suspended state; if is never called, the generator
will never do anything.

* In the second context, activates the generator, which runs
until it hits a yield, a return, or raises an exception.  Whichever
frame calls is considered the parent frame.  Using a single
generator-iterator object across multiple threads is a Bad Idea (with
the usual caveats about careful locking).

(Yes, I think all of this is in the PEP, but I think the PEP could use a
clear summary like this.  Or I suppose I could try writing up something
for Fred to use in the docs -- I don't see anything in the dev docs
about generators.)
                      --- Aahz  <*>  (Copyright 2001 by aahz at

Hugs and backrubs -- I break Rule 6       
Androgynous poly kinky vanilla queer het Pythonista   

"Plus ca change, plus c'est la meme chose."

More information about the Python-list mailing list