class Person: def __init__(self, name, age): self.name = name self.age = age def haha(self): print("哈哈") p = Person("alex", 28) if hasattr(p, "haha"): # attribute 判断xxx模块(对象) 是否有xxx属性 fn = getattr(p, "haha") # 从xxx对象中获取xxx属性 if callable(fn): # 判断是否可以被调用 + () fn() # 哈哈 # delattr(p, "name") # 删除xxx模块(对象)中的xxx属性 # print(p.name) p2 = Person("wusir", 22) print(p2.name) # wusir setattr(p, "address", "北京市八大胡同") # 给xxx模块(对象)设置xxx属性,值为xxx setattr(p, "name", "胡辣汤") print(p.address) # 北京市八大胡同 print(p.name) # 胡辣汤 setattr(p, "chi", lambda : print("我要吃")) p.chi() # 我要吃 setattr(Person, "chi", lambda : print("我要吃")) p.chi() # 我要吃