Here's the simple way to avoid the problem. It avoids the error by converting this kwargs dictionary into a string (along with args), to make an acceptable dictionary key.
I got the idea from that Memoize section of each Python Decorator Library.
import time
def memoize(func):
"""Store the results of the decorated function for fast lookup
"""
# Store results in a dict that maps arguments to results
cache = {}
def wrapper(*args, **kwargs):
key = str(args) + str(kwargs)
if key not in cache:
cache[key] = func(*args, **kwargs)
return cache[key]
return wrapper
@memoize
def slow_function(a, b):
print('Sleeping...')
time.sleep(5)
return a + b
print(slow_function(3,4))
Want to be a master in Data Science? Enroll in this Data Scientist