Created
July 1, 2015 16:18
-
-
Save lordkebab/33684d041ae0533461f1 to your computer and use it in GitHub Desktop.
Python interpreter session playing around with coroutines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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