class A:
pass
A = foo(bar(A))
@foo
@bar
class A:
pass
def decorator(cls):
class Wrapper(object):
def __init__(self, *args):
self.wrapped = cls(*args)
def __getattr__(self, name):
print('Getting the {} of {}'.format(name, self.wrapped))
return getattr(self.wrapped, name)
return Wrapper
@decorator
class C(object):
def __init__(self, x, y):
self.x = x
self.y = y
if __name__ == '__main__':
x = C(1,2)
print(x.x)
Getting the x of <__main__.C object at 0x106b91198>
1
本系列文章和代码已经作为项目归档到github,仓库地址:jumper2014/PyCodeComplete。大家觉得有帮助就请在github上star一下,你的支持是我更新的动力。什么?你没有github账号?学习Python怎么可以没有github账号呢,快去注册一个啦!