Created
January 11, 2012 19:25
-
-
Save begriffs/1596308 to your computer and use it in GitHub Desktop.
Closures in Python bind the same as those in JavaScript
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
def magic(): | |
x = 42 | |
ret = [] | |
for i in range(3): | |
ret.append(lambda: x) | |
x += 1 | |
return ret | |
fs = magic() | |
print fs[0]() | |
print fs[1]() | |
print fs[2]() | |
# results in 45, 45, 45 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment