1、下面是第一个类装饰器
def decorator(cls):
class Wrapper:
def __init__(self, *args):
self.wrapped = cls(*args)
def __getattr__(self, name):
return getattr(self.wrapped, name)
return Wrapper
@decorator
class C:
def __init__(self, x, y):
self.attr = 'spam'
2、下面是第二个类装饰器
class Decorator:
def __init__(self, C):
self.C = C
def __call__(self, *args):
self.wrapped = self.C(*args)
return self
def __getattr__(self, attrname):
return getattr(self.wrapped, attrname)
@Decorator
class C: ...
我看到书上说,第二个类装饰器和第一个不同,第二个没有能够处理给定的类的多个实例——每个实例创建调用都覆盖了前面保存的实例。第一个类装饰器却支持多个实例,因为每个实例创建调用产生了一个新的独立的包装器对象。
请问怎么理解上面这段话,为何第二个类装饰器在每个实例创建时都会覆盖前面保存的实例呢? 感谢指点!