Created
May 20, 2016 12:51
-
-
Save mrfratello/9a9ebf7919ea0e9294dcbf19c59af77b to your computer and use it in GitHub Desktop.
State saver on 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 saver: | |
state = 0 | |
def __init__(self, start): | |
self.state = start | |
def __call__(self, additional): | |
self.state += additional | |
return self.state | |
acc = saver(10) | |
print acc(2) | |
print acc(3) | |
print acc(100) |
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
def saver(start): | |
global state | |
state = start | |
def add(additional): | |
global state | |
state += additional | |
return state | |
return add | |
acc = saver(10) | |
print acc(2) | |
print acc(3) | |
print acc(100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment