标签:class getattr 添加 程序 png asa 科学 alt 计算机
class BlackMedium: feture = "Ugly" def __init__(self, name, addr): self.name = name self.addr = addr def sell_hourse(self): print("{}正在卖房子".format(self.name)) def rent_hourse(self): print("{}正在租房子".format(self.name)) b1 = BlackMedium(‘万成置地‘, ‘万置园‘) print(hasattr(b1,"name")) print(getattr(b1, "sell_hourse")) s = getattr(b1, "sell_hourse")() #运行该方法 print(getattr(b1,"aaaa","没有这个属性哦"))
class BlackMedium: feture = "Ugly" def __init__(self, name, addr): self.name = name self.addr = addr def sell_hourse(self): print("{}正在卖房子".format(self.name)) def rent_hourse(self): print("{}正在租房子".format(self.name)) b1 = BlackMedium(‘万成置地‘, ‘万置园‘) setattr(b1,"jinling","mygirlfriends") #添加属性,等同于 b1.jinling = "mygirlfriends" print(b1.__dict__) setattr(b1,"name", "liu") #修改属性 print(b1.__dict__) setattr(b1, ‘func‘,lambda x : x + 1) #设置函数属性 print(b1.__dict__) print(b1.func(6)) #调用上面设置的函数 delattr(b1,"name") #删除name属性 等同于 del b1.name print(b1.__dict__)
################################
标签:class getattr 添加 程序 png asa 科学 alt 计算机
原文地址:https://www.cnblogs.com/dabai123/p/11595178.html