Created
April 22, 2010 00:52
-
-
Save duboisf/374657 to your computer and use it in GitHub Desktop.
simple closure example in python
This file contains 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
class Value: | |
def __init__(self, val): | |
self.val = val | |
def invoker(x): | |
def closure(y): | |
print (x.val + y) | |
return closure | |
anObject = Value(5) | |
f = invoker(anObject) | |
f(1) | |
anObject.val = 10 | |
f(1) | |
# prints: | |
# 6 | |
# 11 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment