【Python学习之旅】---类的装饰器

shengge0 2020-01-12

#类的装饰原理,自定义一个高阶函数(把函数当做参数传入,返回值也是相同函数地址)def foo(bar):    print(bar)    bar.x=1  #操作Name的属性字典    bar.y=2    return bar@foo     #Name=foo(Name)class Name:    passprint(Name.__dict__) #输出Name的属性字典#一切皆对象,函数也有属性字典@foodef test():    print(‘test函数‘)test()print(test.__dict__)#执行结果:

<class ‘__main__.Name‘>
{‘__module__‘: ‘__main__‘, ‘__dict__‘: <attribute ‘__dict__‘ of ‘Name‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘Name‘ objects>, ‘__doc__‘: None, ‘x‘: 1, ‘y‘: 2}
<function test at 0x000002358FEC8EA0>
test函数
{‘x‘: 1, ‘y‘: 2}

相关推荐