Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayust/8702557 to your computer and use it in GitHub Desktop.
Save ayust/8702557 to your computer and use it in GitHub Desktop.
>>> def echo(s):
... print s
...
>>> funcs = []
>>> for i in range(3):
... funcs.append(lambda: echo(i))
...
>>> for func in funcs:
... func()
...
2
2
2
>>> funcs = []
>>> for i in range(3):
... funcs.append(partial(echo, i))
...
>>> for func in funcs:
... func()
...
0
1
2
@mathieucaroff
Copy link

echo = lambda x: print(x)
funcs = []
for i in range(3):
    funcs.append((lambda i: lambda: echo(i))(i))
for f in funcs:
    f()
#>
# 0
# 1
# 2

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