I came up with a caching decorator for pure functions. Is it ok? Could it be better/simpler/faster?
def cached(f):
def _(*args):
if args in _._cache:
return _._cache[args]
else:
result = f(*args)
_._cache[args] = result
return result
_._cache = {}
return _
lru_cache
function already built into the library. I would suspect it to be faster than this... In other words, if this is part of a bigger scheme I would consider usinglru_cache
instead. \$\endgroup\$lru_cache
has only been added in Python 3.2. \$\endgroup\$