Your decorator syntax:
@hook("test")
def testHook():
# ...
translates to:
def testHook():
# ...
testHook = hook("test")(testHook)
so it is given just one argument. You need to restructure your code to produce your class decorator as a return value of hook()
.
The following would work:
def hook(hookname):
class HookDecorator(object):
def __init__(self, f):
if hookname not in hooks:
hooks[hookname] = []
hooks[hook].append({"module": f.__module__, "func": f})
self.f = f
def __call__(self, *args):
return self.f(*args)
return HookDecorator
where the __call__
uses self.f
, not the global f
, and returns whatever the decorated function produced. I'm assuming that hooks
is a global dictionary somewhere.
There is little point in making this a class however; you use very little state. You could just as well use a wrapper function here:
from functools import wraps
def hook(hookname):
def decorator(f):
if hookname not in hooks:
hooks[hookname] = []
hooks[hookname].append({"module": f.__module__, "func": f})
@wraps(f)
def wrapper(*args):
return f(*args)
return wrapper
return decorator