question about generators

Paul Rubin phr-n2002b at NOSPAMnightsong.com
Thu Aug 15 23:54:10 CEST 2002


Andrew Koenig <ark at research.att.com> writes:
> Steve> But don't you think this is because of the fundamentally
> Steve> different nature of "print" and "yield"?
> 
> At one level, of course I do -- but on another level, I don't.
> 
> A generator is a program that yields a sequence of values
> to its caller.  If I have a program that prints a sequence
> of values -- that is, a program that yields a sequence of
> values to the printer -- one might think that by changing all
> of the print statements to yield statements (ignoring formatting
> issues, of course), one could change one program to another.

The problem you're hitting is that generators do what you expect, but
Python doesn't support generators--it only supports a limited type of
generator called a "simple generator", which can only yield directly
to its caller.  Its callees cannot yield through it. 

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; I don't know if it does so though.

Anyway, the problem you're having where yield doesn't really work like
print comes from this implementation limitation, not anything inherent
in the notion of generators.



More information about the Python-list mailing list