Optimizing a memoization decorator not increase call stack
Posted
by
Tyler Crompton
on Stack Overflow
See other posts from Stack Overflow
or by Tyler Crompton
Published on 2012-09-19T21:36:34Z
Indexed on
2012/09/19
21:37 UTC
Read the original article
Hit count: 247
python
|python-3.x
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?
© Stack Overflow or respective owner