E.g., I have:
def readDb():
# Fetch a lot of data from db, spends a lot time
...
return aList
def calculation():
x = readdb()
# Process
Write a simple decorator:
class memo(object):
def __init__(self, fun):
self.fun = fun
self.res = None
def __call__(self):
if self.res is None:
self.res = self.fun()
return self.res
@memo
def readDb():
# ... etc
return aList
For more general solutions, look here: http://code.activestate.com/recipes/498245-lru-and-lfu-cache-decorators/.