首页 > 编程语言 > 详细

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

时间:2020-01-12 19:20:30      阅读:76      评论:0      收藏:0      [点我收藏+]
#类的装饰原理,自定义一个高阶函数(把函数当做参数传入,返回值也是相同函数地址)
def foo(bar):
print(bar)
bar.x=1 #操作Name的属性字典
bar.y=2
return bar
@foo #Name=foo(Name)
class Name:
pass

print(Name.__dict__) #输出Name的属性字典

#一切皆对象,函数也有属性字典
@foo
def 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}

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

原文:https://www.cnblogs.com/chenyuxia/p/12183224.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!