【发布时间】:2017-03-08 00:52:03
【问题描述】:
这是我的 memoize 实现:
猫测试.py
def _memoize(obj):
cache = obj.cache = {}
@functools.wraps(obj)
def memoizer(*args, **kwargs):
key = str(args) + str(kwargs)
if key not in cache:
cache[key] = obj(*args, **kwargs)
return cache[key]
return memoizer
@_memoize
class Test(object):
def __init__(self, arg1):
super(Test, self).__init__()
self.arg = arg1
print "init executed for " + arg1
def authenticate(self):
print self.arg
t1 = Test('a')
当我运行它时,我得到以下错误:
$python test.py
Traceback (most recent call last):
File "test.py", line 23, in <module>
t1 = Test('a')
File "test.py", line 9, in memoizer
cache[key] = obj(*args, **kwargs)
File "test.py", line 16, in __init__
super(Test, self).__init__()
TypeError: super() argument 1 must be type, not function
您能否建议如何解决此错误?
【问题讨论】:
-
所以你总是希望
Test(x) is Test(y)在x == y时为真(或者至少在hash(x) == hash(y)时)?
标签: python python-2.7 class memoization