python3 函数中的反射

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'}

  

原文地址:https://www.cnblogs.com/xp1005/p/6529796.html