Some memory improvements of @cachedmethod

This commit is contained in:
Miroslav Stampar
2017-11-09 12:24:11 +01:00
parent 58b87e4b6b
commit 9404b63a42
4 changed files with 8 additions and 7 deletions

View File

@@ -14,11 +14,11 @@ def cachedmethod(f, cache={}):
def _(*args, **kwargs):
try:
key = (f, tuple(args), frozenset(kwargs.items()))
key = hash((f, tuple(args), frozenset(kwargs.items())))
if key not in cache:
cache[key] = f(*args, **kwargs)
except:
key = "".join(str(_) for _ in (f, args, kwargs))
key = hash("".join(str(_) for _ in (f, args, kwargs)))
if key not in cache:
cache[key] = f(*args, **kwargs)