question about generators
Andrew Koenig
ark at research.att.com
Thu Aug 15 18:48:01 EDT 2002
Paul> The problem you're hitting is that generators do what you
Paul> expect, but Python doesn't support generators--it only supports
Paul> a limited type of generator called a "simple generator", which
Paul> can only yield directly to its caller. Its callees cannot yield
Paul> through it.
Paul> Implementing generators requires something like Scheme
Paul> continuations, which save the whole call stack up to the yield
Paul> point, and switch back to it when the generator is resumed.
Paul> That was considered infeasible in Jython and a pain in the neck
Paul> in CPython, hence the limitation to simple generators, which
Paul> save only a single stack frame (that's just another Python heap
Paul> object). Stackless Python could implement full generators that
Paul> do what you want them to; I don't know if it does so though.
Ah, I see.
Paul> Anyway, the problem you're having where yield doesn't really
Paul> work like print comes from this implementation limitation, not
Paul> anything inherent in the notion of generators.
To complicate matters, I now realize that my mental model of what was
happening is wrong anyway. I said earlier that I imagined the first
yield in a generator being different from the others, but what is
actually happening is that the mere textual appearance of a yield
statement in a function causes the function to return an iterator
immediately.
This example shows the difference:
def f():
print "Hello"
yield 1
>>> a = f()
>>> a.next()
Hello
1
In my previous model, I would have expected the call to f() to print
Hello and then return an iterator.
--
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark
More information about the Python-list
mailing list