通过将装饰器定义为类来装饰类的简单示例是什么?
我正在尝试使用PEP 3129实现在Python 2.6中实现的功能,除了使用类而不是函数,因为Bruce Eckel解释here。
以下作品:
class Decorator(object):
def __init__(self, arg):
self.arg = arg
def __call__(self, cls):
def wrappedClass(*args):
return cls(*args)
return type("TestClass", (cls,), dict(newMethod=self.newMethod, classattr=self.arg))
def newMethod(self, value):
return value * 2
@Decorator("decorated class")
class TestClass(object):
def __init__(self):
self.name = "TestClass"
print "init %s"%self.name
def TestMethodInTestClass(self):
print "test method in test class"
def newMethod(self, value):
return value * 3
除此之外,在上面,wrappedClass不是一个类,而是一个被操作以返回类类型的函数。我想写相同的可调用如下:
def __call__(self, cls):
class wrappedClass(cls):
def __init__(self):
... some code here ...
return wrappedClass
如何做到这一点?
我不完全确定“”“......这里的一些代码......”“”
答案 0 :(得分:15)
如果您想覆盖new_method()
,请执行以下操作:
class Decorator(object):
def __init__(self, arg):
self.arg = arg
def __call__(self, cls):
class Wrapped(cls):
classattr = self.arg
def new_method(self, value):
return value * 2
return Wrapped
@Decorator("decorated class")
class TestClass(object):
def new_method(self, value):
return value * 3
如果您不想改变__init__()
,则无需覆盖它。
答案 1 :(得分:4)
在此之后,类NormalClass成为ClassWrapper 实例:
def decorator(decor_arg):
class ClassWrapper:
def __init__(self, cls):
self.other_class = cls
def __call__(self,*cls_ars):
other = self.other_class(*cls_ars)
other.field += decor_arg
return other
return ClassWrapper
@decorator(" is now decorated.")
class NormalClass:
def __init__(self, name):
self.field = name
def __repr__(self):
return str(self.field)
测试:
if __name__ == "__main__":
A = NormalClass('A');
B = NormalClass('B');
print A
print B
print NormalClass.__class__
输出:
A is now decorated. <br>
B is now decorated. <br>
\__main__.classWrapper