Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- The memoize decorator saves arguments and then compares it
- with previous arguments. If the arguments match, it returns the
- result from the calculation when the arguments where
- already used.
- by Niklas Rosenstein
- info@nux95.com
- """
- def memoize(steps=25):
- results = {'args':[],'result':[]}
- def wrapper(f):
- def nf(*args,**kwargs):
- if (args,kwargs) in \
- results["args"]:
- return results["result"]\
- [results["args"]\
- .index((args,kwargs))]
- else:
- results["args"].append(
- (args,kwargs) )
- r = f(*args,**kwargs)
- results["result"].append(r)
- if len(results["result"]) \
- > steps:
- del results["args"][0]
- del results["result"][0]
- return r
- nf.__name__ = f.__name__
- return nf
- return wrapper
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement