3
我有一個簡單的Python類,我想用它來爲我寫的程序添加命名的鉤子。我嘗試運行下面的代碼,並得到以下輸出。帶參數的Python Decorator類
代碼:
hooks = {}
class hook(object):
def __init__(self, f, hook):
if hook not in hooks:
hooks[hook] = []
hooks[hook].append({"module": f.__module__, "func": f})
self.f = f
def __call__(self, *args):
f(*args)
@hook("test")
def testHook():
print "hi"
輸出:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() takes exactly 3 arguments (2 given)
我該如何解決這個問題?我使用Python 2.7
我明白了。非常感謝! :) –