码迷,mamicode.com
首页 > 其他好文 > 详细

2019年9月22日 类的装饰器

时间:2019-09-22 21:28:48      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:int   fun   class   one   testing   UNC   dict   就是   foo   

装饰器本身就是函数 

回顾:

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}

 

2019年9月22日 类的装饰器

标签:int   fun   class   one   testing   UNC   dict   就是   foo   

原文地址:https://www.cnblogs.com/python1988/p/11569029.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!