装饰器本身就是函数
回顾:
def deco(func): print(‘>>deco>>‘) return func @deco # 做了 这么一件事情:test=deco(test) def test(): print(‘testing‘) test()
》》》》》》
>>deco>>
testing
def deco(obj): print(‘>>deco>>‘,obj) obj.x=1 obj.y=2 obj.z=3 return obj @deco #Foo=deco(Foo) class Foo: pass f1=Foo() print(Foo.__dict__)
>>>>>>
>>deco>> <class ‘__main__.Foo‘>
{‘__module__‘: ‘__main__‘, ‘__dict__‘: <attribute ‘__dict__‘ of ‘Foo‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘Foo‘ objects>, ‘__doc__‘: None, ‘x‘: 1, ‘y‘: 2, ‘z‘: 3}
def deco(obj): print(‘>>deco>>‘,obj) obj.x=1 obj.y=2 obj.z=3 return obj @deco def test():#验证一切皆对象 print(‘testing‘) print(test.__dict__)
》》》》
>>deco>> <function test at 0x10203d048>
{‘x‘: 1, ‘y‘: 2, ‘z‘: 3}
原文地址:https://www.cnblogs.com/python1988/p/11569029.html
时间: 2024-11-09 02:38:00