Skip to content

Instantly share code, notes, and snippets.

@stucchio
Created October 17, 2010 15:00
Show Gist options
  • Save stucchio/630921 to your computer and use it in GitHub Desktop.
Save stucchio/630921 to your computer and use it in GitHub Desktop.
Python coroutines
def cr():
state = False
for i in range(100):
new_state = None
if state:
new_state = (yield i)
else:
new_state = (yield "Off")
if not (new_state is None):
state = new_state
c = cr()
assert(c.next() == 'Off')
assert(c.next() == 'Off')
assert(c.send(True) == 2)
assert(c.next() == 3)
assert(c.next() == 4)
assert(c.send(False) == 'Off')
assert(c.next() == 'Off')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment