Created
March 4, 2015 19:25
-
-
Save zmanji/02dc5870dcbcee0c62c6 to your computer and use it in GitHub Desktop.
Closure creation in Python
This file contains hidden or 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
[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