Created
October 17, 2010 15:00
-
-
Save stucchio/630921 to your computer and use it in GitHub Desktop.
Python 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 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