1. Reflection consists of four functions hasattr (), GetAttr (), SetAttr (), delattr ()
2.hasattr (O,name) determine if the name attribute or method exists in the class
class test(object): def __init__(self): self.name=‘张三‘ def show(self): print(‘姓名:%s‘%self.name)T=test()print(hasattr(T,‘show‘))print(hasattr(T,‘name‘))print(hasattr(T,‘name1‘))>>:TrueTrueFalse
3.getattr (o,name) Gets the property or method in the corresponding class based on the name string
class test(object): def __init__(self): self.name=‘张三‘ def show(self): print(‘姓名:%s‘%self.name)T=test()res=getattr(T,‘show‘)res()>>:姓名:张三
4.setattr (O,name,value) Setting property values for a class
class test(object): def __init__(self): self.name=‘张三‘ def show(self): print(‘姓名:%s‘%self.name)T=test()setattr(T,‘name‘,‘李四‘)print(T.name)>>:李四
5.delattr (o,name) Delete a property or method of a class
class test(object): def __init__(self): self.name=‘张三‘ def show(self): print(‘姓名:%s‘%self.name)T=test()delattr(T,‘name‘)print(T.name)>>:报错
Python object-oriented-reflection