- __get__魔法方法
- 将 类方法 转化成 类属性
- 装饰 另一个类的类方法
class TestDes:
def __init__(self, func):
self.func = func
def __get__(self, instance, owner):
# instance是 被装饰类 的实例,owner 为 被装饰类
print(instance, owner)
p = self.func(instance)
return 'hello'
class TestMain:
@TestDes
def pp(self):
return '100'
if __name__ == '__main__':
t = TestMain()
print(t.pp)
# 结果:
# < __main__.TestMain object at 0x0000028943386640 > <class '__main__.TestMain'>
# hello