Skip to content

Instantly share code, notes, and snippets.

@tangentstorm
Created October 1, 2012 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tangentstorm/3814598 to your computer and use it in GitHub Desktop.
Save tangentstorm/3814598 to your computer and use it in GitHub Desktop.
when generators share their state
"""
This program generates the following table:
01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
It shows what happens when two generators share the same state.
Contrast with: https://gist.github.com/3814556
"""
def each( seq ):
"""
This does exactly what the built-in iter() function does.
iter() is also invoked by the 'for' loop itself, so it's
doubly redundant.
"""
for e in seq: yield e
gen = each('0123456789ABCDEF')
for y in gen:
for x in gen:
print ( y + x ),
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment