Skip to content

Instantly share code, notes, and snippets.

@begriffs
Created January 11, 2012 19:25
Show Gist options
  • Save begriffs/1596308 to your computer and use it in GitHub Desktop.
Save begriffs/1596308 to your computer and use it in GitHub Desktop.
Closures in Python bind the same as those in JavaScript
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