Stackless Python and Python 2.x

Tim Peters at
Sun Sep 2 04:27:49 CEST 2001

[Paul Rubin]
> How is it possible to implement resumable generators without using
> coroutines, continuations, or something equivalent?

Have you read the PEP?

This flavor of generator is formally a semi-coroutine, and it's the
asymmetry that makes impementation easier; a PEP 255 generator can suspend
only to the routine that invoked it, and only to the point of invocation,
which implies that nothing more than the generator's local state needs to be
preserved between resumptions.  In Python terms, since execution frames were
already heap-allocated, it’s not much of an exaggeration to say that the
primary trick to implementing generators was simply to refrain from
decrementing a frame’s refcount upon yielding.

BTW, the book “Advanced Programming Language Design” is available online:,3833,0805311912+20,00.html

and chapter 2 shows in excruciatingly detail how to implement CLU iterators
(also semi-coroutines) using almost-standard C, abusing the C stack via
setjmp/longjmp tricks.

More information about the Python-list mailing list