Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为其init具有super()初始化的类进行memonize_Python_Python 2.7_Class_Memoization - Fatal编程技术网

Python 为其init具有super()初始化的类进行memonize

Python 为其init具有super()初始化的类进行memonize,python,python-2.7,class,memoization,Python,Python 2.7,Class,Memoization,以下是我对memoize的实现: cat test.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)

以下是我对memoize的实现:

cat test.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.py”,第23行,在
t1=测试('a')
文件“test.py”,第9行,在Memorizer中
cache[key]=obj(*args,**kwargs)
文件“test.py”,第16行,在_init中__
超级(测试,自我)。\uuuu初始化
TypeError:super()参数1必须是类型,而不是函数

您能建议如何解决此错误吗?

functools.wrapps
是函数包装器的一种方便方式,使用decorator是

因此,Test不再是一个类,而是一个函数,正如错误所示,
super
不需要函数


我不太理解您提出替代方案的意图。

functools.wrapps
对于函数包装器来说是一种方便,而使用decorator则是

因此,Test不再是一个类,而是一个函数,正如错误所示,
super
不需要函数


我不太理解您提出替代方案的意图。

在您的Memorizer功能中,您需要创建一个新类型;您正在创建并返回一个函数,这样做就是将类转化为一个函数。一种更简单的方法是重写
\uuuuu new\uuuuuu
,它允许您在分配对象之前拦截对构造函数的调用,因此您可以这样做(简化了,但也可以在此过程中复制多参数处理):


如果你想要更具装饰风格的东西,你可以看看
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
,它允许你以一种更容易在类间共享而无需继承的方式来完成类似的事情。

在你的记忆器函数中,你需要创建一个新的类型;您正在创建并返回一个函数,这样做就是将类转化为一个函数。一种更简单的方法是重写
\uuuuu new\uuuuuu
,它允许您在分配对象之前拦截对构造函数的调用,因此您可以这样做(简化了,但也可以在此过程中复制多参数处理):

如果你想要更具装饰风格的东西,你可以看看
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu元类>/code>,它允许你以一种更容易在类间共享而无需继承的方式来做类似的事情。

所以你总是希望
x==y
时,Test(x)是Test(y)
为真所以当
x==y
(或者至少,当
hash(x)==hash(y)
)时,您总是希望
Test(x)是Test(y)
Test = _memoize(Test)
class Test(object):
    def __init__(self, arg):
        self.x = arg

    _cache = {}
    def __new__(cls, arg):
        if arg not in _cache:
            _cache[arg] = cls(arg)
        return _cache[arg]