标签:ict sel obj style ini 添加 通过 def elf
class Foo: def __init__(self,name,age): self.name = name self.age = age def show(self): return "%s%s"%(self.name,self.age) obj = Foo("alex",22) # print(obj.name) #获取到name值 # print(obj.__dict__["name"]) #获取到name值 r = getattr(obj,"name") #反射方法获取name值 print(r) g = getattr(obj,"show") #在对象中通过反射方式getattr凡是找到show方法 print(g()) #执行show方法 h = hasattr(obj,"name") #反射之hasattr用来判断是否有这个参数 print(h) s = setattr(obj,"cctv","bbcs") #反射之setattr用来添加字段 print(getattr(obj,"cctv")) print(obj.cctv,"ggggggggg") print(obj.__dict__) #delattr(obj,"name") #反射之delattr用来删除字段 # print(obj.name)
标签:ict sel obj style ini 添加 通过 def elf
原文地址:https://www.cnblogs.com/TKOPython/p/12382787.html