Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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