Skip to content

Instantly share code, notes, and snippets.

@fcostin
Created August 23, 2012 13:31
Show Gist options
  • Save fcostin/3436628 to your computer and use it in GitHub Desktop.
Save fcostin/3436628 to your computer and use it in GitHub Desktop.
attack of generator.send
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