Optimizing a memoization decorator not increase call stack
- by Tyler Crompton
I have a very, very basic memoization decorator that I need to optimize below:
def memoize(function):
memos = {}
def wrapper(*args):
try:
return memos[args]
except KeyError:
pass
result = function(*args)
memos[args] = result
return result
return wrapper
The goal is to make this so that it doesn't add on to the call stack. It actually doubles it right now. I realize that I can embed this on a function by function basis, but that is not desired as I would like a global solution for memoizing. Any ideas?