Stackless Python and Python 2.x
aahz at panix.com
Wed Sep 5 16:35:24 CEST 2001
[I'll take a whack at this and Tim will correct me if I'm wrong.]
In article <Xns911368A40610Dmichaelrcpcouk at 188.8.131.52>,
Michael Abbott <michael at rcp.co.uk> wrote:
>"Tim Peters" <tim.one at home.com> wrote in
>news:mailman.999678188.20206.python-list at python.org:
>> What part of Simple Generators does anyone not yet understand?
>How does the Python interpreter maintain the state of a generator during
>calls to yield? Evidently the entire state has to be restored from
>wherever it's being kept and plonked back onto the current stack.
The actual execution frame (the part that stores all the local names) is
stored on the heap, not on the stack. It's a Python object, like any
other Python object, with all the usual refcounting rules. Normally,
when Python returns from a function call, it decrefs the frame, and
because there's only one refcount, it gets deleted. With generators,
though, the yield does *NOT* decref the frame, so it sticks around.
That quite literally is all there is to it, modulo some implementation
The part I don't understand (though if I had access to 2.2a2, I could
probably test it easily enough) is what happens when you re-enter a
generator and trigger an exception (an exception that isn't
StopIteration, for any smartasses out there).
--- Aahz <*> (Copyright 2001 by aahz at pobox.com)
Hugs and backrubs -- I break Rule 6 http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het Pythonista
"Plus ca change, plus c'est la meme chose."
More information about the Python-list