Skip to content

Instantly share code, notes, and snippets.

@seungha-kim
Last active December 4, 2015 07:27
Show Gist options
  • Save seungha-kim/7228a296d161ba089fe6 to your computer and use it in GitHub Desktop.
Save seungha-kim/7228a296d161ba089fe6 to your computer and use it in GitHub Desktop.
Lazy evaluation of Python generator
foo = 1
# expected [1, 1, 1]
gen = (foo for i in range(3))
# but...
foo = 2
list(gen)
# result: [2, 2, 2]
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