Created
August 23, 2012 13:31
-
-
Save fcostin/3436628 to your computer and use it in GitHub Desktop.
attack of generator.send
This file contains hidden or 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
In [10]: def adventure(): | |
....: print 'there is cupcake. do you eat it?' | |
....: x = (yield) | |
....: if x: | |
....: print 'the cupcake is tasty' | |
....: print 'is this awesome?' | |
....: x = (yield) | |
....: if x: | |
....: print 'awesome!!' | |
....: while True: | |
....: (yield) | |
....: print 'AWESOME!' | |
....: else: | |
....: while True: | |
....: print 'sadness' | |
....: (yield) | |
....: else: | |
....: print 'not eating the cupcake is poison!' | |
....: print 'you die' | |
....: while True: | |
....: print 'you are dead' | |
....: (yield) | |
....: | |
In [11]: a = adventure() | |
In [12]: a.next() | |
there is cupcake. do you eat it? | |
In [13]: a.send(True) | |
the cupcake is tasty | |
is this awesome? | |
In [14]: a.send(False) | |
sadness | |
In [15]: a.send('huh?') | |
sadness |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment