Created
September 16, 2013 06:47
-
-
Save Hardtack/6577333 to your computer and use it in GitHub Desktop.
Lexical scope problem
This file contains 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
functions = [] | |
for val in ['foo', 'bar', 'baz']: | |
def f(): | |
return val | |
functions.append(f) | |
for func in functions: | |
print func() |
This file contains 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
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