classFoo(object):
def__init__(self,name,age):
self.name = name
self.age = age
defshow(self):
return"%s-%s"%(self.name,self.age)
obj = Foo("sunziheng",20)
f = obj.show()
print(f)
func =getattr(obj,"show")
print(func())
v =getattr(obj,"name")
print(v)
print(hash(obj))
print(hasattr(obj,"sunziheng"))# False 判断这个对象里面有没有这个属性 有返回 Ture 没有返回False
print(hasattr(obj,"age"))#True
setattr(obj,'k1','v1')# 给对象设置一个 k1 = v1 的属性
print(obj.k1)
#print(obj.v1) #AttributeError: 'Foo' object has no attribute 'v1'
# delattr(obj,"name") # 删除对象属性
# print(obj.name)
a =20
obj.c = a#Python 可以直接给对象设置属性
print(obj.c)
print(hasattr(obj,"c"))