Skip to content

Instantly share code, notes, and snippets.

@dfm
Created November 20, 2012 22:45
Show Gist options
  • Save dfm/4121783 to your computer and use it in GitHub Desktop.
Save dfm/4121783 to your computer and use it in GitHub Desktop.
y u suck so much?
fs = []
for i in range(10):
fs.append(lambda: i)
print [f() for f in fs]
fs = []
for i in range(10):
def f(j):
return lambda: j
fs.append(f(i))
print [f() for f in fs]
@dfm
Copy link
Author

dfm commented Nov 20, 2012

What is the output....?

@dfm
Copy link
Author

dfm commented Nov 20, 2012

Iloveclosures

@ghl3
Copy link

ghl3 commented Nov 21, 2012

This stuff is always a pain. Your solution is nice, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment