Skip to content

Instantly share code, notes, and snippets.

@cabiad
Created March 23, 2013 12:54
Show Gist options
  • Save cabiad/5227630 to your computer and use it in GitHub Desktop.
Save cabiad/5227630 to your computer and use it in GitHub Desktop.
A tiny coroutine example in python
#########################################################
# #
# 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