Created
October 21, 2011 06:54
-
-
Save illume/1303258 to your computer and use it in GitHub Desktop.
# closures 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
# closures in python | |
def f0(x): | |
closed_var = x + 1 | |
def fclose(): | |
return closed_var | |
return fclose | |
a_closure = f0(9) | |
# the 'closed_var' is now x + 1 == 10 | |
assert(a_closure() == 10) | |
assert(a_closure() == 10) | |
a_closure2 = f0(20) | |
# the 'closed_var' is now x + 1 == 21 | |
assert(a_closure2() == 21) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment