question about generators

greg greg at cosc.canterbury.ac.nz
Fri Aug 16 11:06:55 CEST 2002


Paul Rubin wrote:
> 
> Implementing generators requires something like Scheme continuations,
> which save the whole call stack up to the yield point, and switch back
> to it when the generator is resumed.  That was considered infeasible
> in Jython and a pain in the neck in CPython, hence the limitation to
> simple generators, which save only a single stack frame (that's just
> another Python heap object).  Stackless Python could implement full
> generators that do what you want them to;

No, no, no. You *don't* need Stackless-ness to implement
Icon-style generators. CPython *could* have been made to
do them (although it would have been a lot of work).

However, as Tim pointed out in another post, Python's
generators can actually do something that Icon's can't:
you can have multiple Python generators on the go in
parallel. This is possible because it uses a sort of
one-level-deep version of Stackless-ness, keeping a
Python frame object around for each active generator.

So, although you lose something the Python way, you
gain something too. This is another reason why it's
a bit misleading to call Python's generators "simpler"
than Icon's.

Greg Ewing



More information about the Python-list mailing list