首页 > 编程语言 > 详细

python3 函数中的反射

时间:2017-03-10 12:20:19      阅读:171      评论:0      收藏:0      [点我收藏+]
class Foo:
    def __init__(self,name):
        self.name=name

    def func(self):
        print(‘--------------.func‘)


print(hasattr(Foo,‘func‘))
f=Foo(‘egon‘)
print(hasattr(f,‘x‘))
f.x=1
print(getattr(f,‘x‘))
print(getattr(f,‘func‘))
if hasattr(f,‘func‘):
    aa=getattr(f,‘func‘)
    aa()
print(getattr(f,‘y‘,None))

# f.y=1 #f y 1
setattr(f,‘y‘,1)
print(f.__dict__)

delattr(f,‘y‘)
print(f.__dict__)


# print(Foo.__dict__)
# print(f.__dict__)

  

## 打印
True
False
1
<bound method Foo.func of <__main__.Foo object at 0x000001B32824D0F0>>
--------------.func
None
{‘x‘: 1, ‘y‘: 1, ‘name‘: ‘egon‘}
{‘x‘: 1, ‘name‘: ‘egon‘}

  

python3 函数中的反射

原文:http://www.cnblogs.com/xp1005/p/6529796.html

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