Skip to content

Instantly share code, notes, and snippets.

@lordkebab
Created July 1, 2015 16:18
Show Gist options
  • Save lordkebab/33684d041ae0533461f1 to your computer and use it in GitHub Desktop.
Save lordkebab/33684d041ae0533461f1 to your computer and use it in GitHub Desktop.
Python interpreter session playing around with coroutines
>>> def gen():
... while True:
... x = (yield)
... print(x)
...
>>> a = gen()
>>> a.send('hi')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't send non-None value to a just-started generator
>>>
>>> def consumer(func):
... def wrapper(*args, **kw):
... f = func(*args, **kw)
... next(f)
... return f
... return wrapper
...
>>> @consumer
... def another_gen():
... while True:
... x = (yield)
... print(x)
...
>>> b = another_gen()
>>> b.send('hi')
hi
#
# the consumer decorator primes the coroutine
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment