Created
March 23, 2013 12:54
-
-
Save cabiad/5227630 to your computer and use it in GitHub Desktop.
A tiny coroutine example in python
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
######################################################### | |
# # | |
# Copyright 2013 Christopher Abiad All rights reserved. # | |
# # | |
######################################################### | |
__author__ = 'Chris Abiad <chris@chrisabiad.com>' | |
def f1(): | |
while True: | |
print "waiting for value from <generator object>.send()" | |
next_val = yield | |
print "yielding received value {}".format(next_val) | |
yield next_val | |
if __name__ == '__main__': | |
y = f1() | |
y.next() | |
for i in range(5): | |
print "send()ing {}".format(i) | |
val = y.send(i) | |
print "got back yielded val {}".format(val) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment