Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
Created December 9, 2010 08:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DmitrySoshnikov/734482 to your computer and use it in GitHub Desktop.
Save DmitrySoshnikov/734482 to your computer and use it in GitHub Desktop.
No closure for unused vars, even with eval
# 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