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"))