I need to make a decorator that checks if the function that its associated with was called before with exactly the same arguments as its being called with now. If it was, I need to return the previous output. If not I call it and store the result for future calls. Here is what I have:
class memoized(object):
def __init__(self,f):
self.__first=[]
self.__last=[]
self.__f=f
self.__name__=f.__name__
self.__value=[]
def __call__(self,*args,**dargs):
for i in self.__value:
if(self.__first[i]==args and self.__last[i]==dargs):
return self.__value[i]
rv=self.__f(*args,**dargs)
self.__first.append(args)
self.__last.append(dargs)
self.__value.append(rv)
return rv
when I run it though it gives me an idex error. Im not sure why since theoretically the length of value first and last should be the same all the time because I append to all 3 of them. Any ideas?