【发布时间】:2021-02-17 16:38:55
【问题描述】:
我有这个代码,当我打印出来时出现这个错误,谁能告诉我如何解决这个问题?
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 wraper(*args, **kwargs):
if (args, kwargs) not in cache:
cache[(args, kwargs)] = func(*args, **kwargs)
return cache[(args, kwargs)]
return wraper
@memoize
def slow_function(a, b):
print('Sleeping...')
time.sleep(5)
return a + b
print(slow_function(3,4))
错误:TypeError: unhashable type: 'dict'
【问题讨论】:
-
Highly related,但我不认为是骗子(见第一个答案)。
标签: python function python-decorators