Skip to content

Instantly share code, notes, and snippets.

@sahib
Created December 17, 2012 22:32
Show Gist options
  • Save sahib/4323010 to your computer and use it in GitHub Desktop.
Save sahib/4323010 to your computer and use it in GitHub Desktop.
>>> def co():
... try:
... while True:
... item = yield
... print('Received:', item)
... except GeneratorExit:
... print('Exit.')
...
...
...
>>> g = co()
>>> g
<generator object co at 0x7fd3ce6e7a00>
>>> g.send(None) # Starte Generator.
>>> g.send('Berta')
Received: Berta
>>> g.send('Klaus')
Received: Klaus
>>> g.send('Seb')
Received: Seb
>>> g.send('')
Received:
>>> g.close() # werfe GeneratorExit innerhalb von co
Exit.
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment