Created
December 9, 2010 08:32
-
-
Save DmitrySoshnikov/734482 to your computer and use it in GitHub Desktop.
No closure for unused vars, even with eval
This file contains hidden or 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
# Python does not save not used bindings in | |
# the closured environment. However, even | |
# `eval` doesn't help to save them. | |
# | |
# In contrast, ECMAScript having environments | |
# frames, normaly find variable "x", see | |
# the same ES example here: https://gist.github.com/734485 | |
# | |
# by Dmitry A. Soshnikov | |
# | |
# See detailed explanation of closures | |
# in Python here: https://gist.github.com/700292 | |
# | |
def foo(x): | |
def bar(y): | |
print(eval(k)) | |
return bar | |
bar = foo(10) | |
print(bar.__closure__) # None | |
k = "y" | |
bar(20) # OK, 20 | |
k = "x" | |
bar(20), # error, "x" is not defined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment