我想使用装饰器对派生类做一些事情(例如注册类或其他东西)。这是我的代码:
from functools import wraps
class Basic(object):
def __init__(self):
print "Basic::init"
def myDeco(name):
# define the decorator function that acts on the actual class definition
def __decorator(myclass):
# do something here with the information
print name, myclass
# do the wrapping of the class
@wraps(myclass)
def __wrapper(*args, **kwargs):
return myclass( *args, **kwargs)
# return the actual wrapper here
return __wrapper
# return the decorator to act on the class definition
return __decorator
@myDeco("test")
class Derived(Basic):
def __init__(self):
super(Derived, self).__init__()
print "Derived::init"
instance = Derived()
这给出了以下错误:
TypeError: must be type, not function
当调用super
in 方法时Derived
。我假设变量Derived
不再是 a type
,而是函数__decorator
实际上。
我如何需要更改装饰器(并且只有装饰器)才能解决此问题?