Skip to content

Instantly share code, notes, and snippets.

@mrfratello
Created May 20, 2016 12:51
Show Gist options
  • Save mrfratello/9a9ebf7919ea0e9294dcbf19c59af77b to your computer and use it in GitHub Desktop.
Save mrfratello/9a9ebf7919ea0e9294dcbf19c59af77b to your computer and use it in GitHub Desktop.
State saver on Python
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)
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