Re:Co-routines as an alternative to state machines
by abecedarius on Tuesday September 01, 2009 @12:46AM
(#797406)
I followed the language of Beazley's slides on Python coroutines: a 'generator' is a thing like this:
def g():
for i in range(10):
yield i
and a 'coroutine' is a thing like this:
@coroutine
def c():
while True:
x = yield
print x
The code in the original post is like the latter. g() produces successive outputs when you call .next() on it; c() consumes successive inputs when you call .send(input) on it. The g() style has been in Python longer and is better integrated into the language -- you can say "for x in g():", for example. That's the point I was making, maybe too tersely.
Your point 2 is still correct, though there's a PEP to make trampolining to work around it work nicer.
Your point 2 is still correct, though there's a PEP to make trampolining to work around it work nicer.