Skip to content

Instantly share code, notes, and snippets.

@Hardtack
Created September 16, 2013 06:47
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Hardtack/6577333 to your computer and use it in GitHub Desktop.
Lexical scope problem
functions = []
for val in ['foo', 'bar', 'baz']:
def f():
return val
functions.append(f)
for func in functions:
print func()
functions = []
for val in ['foo', 'bar', 'baz']:
def g(val):
def f():
return val
return f
functions.append(g(val))
for func in functions:
print func()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment