Created
October 1, 2012 21:37
-
-
Save tangentstorm/3814598 to your computer and use it in GitHub Desktop.
when generators share their state
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
""" | |
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 ), | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment