Skip to content

Instantly share code, notes, and snippets.

@zmanji
Created March 4, 2015 19:25
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 zmanji/02dc5870dcbcee0c62c6 to your computer and use it in GitHub Desktop.
Save zmanji/02dc5870dcbcee0c62c6 to your computer and use it in GitHub Desktop.
Closure creation in Python
[tw-mbp-zmanji ~]$ python
Python 2.7.8 (default, Dec 3 2014, 19:02:43)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>> names = ["a", "b", "c"]
>>> callables = []
>>> for n in names:
... callables.append(lambda: print(n))
...
>>> for c in callables:
... c()
...
c
c
c
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment