Last active
December 4, 2015 07:27
-
-
Save seungha-kim/7228a296d161ba089fe6 to your computer and use it in GitHub Desktop.
Lazy evaluation of Python generator
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
foo = 1 | |
# expected [1, 1, 1] | |
gen = (foo for i in range(3)) | |
# but... | |
foo = 2 | |
list(gen) | |
# result: [2, 2, 2] |
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
from itertools import chain | |
list(chain(*((print(a, b) for b in range(2)) for a in range(2)))) | |
# expected : | |
# 0 0 | |
# 0 1 | |
# 1 0 | |
# 1 1 | |
# [None, None, None, None] | |
# result : | |
# 1 0 | |
# 1 1 | |
# 1 0 | |
# 1 1 | |
# [None, None, None, None] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment