面向对象(__item__)
1 #Author : Kelvin 2 #Date : 2019/1/20 21:37 3 class People: 4 def __getitem__(self, item): 5 print("getitem...") 6 return self.__dict__[item] 7 def __setitem__(self, key, value): 8 print("setitem...") 9 self.__dict__[key]=value 10 def __delitem__(self, key): 11 print("delitem...") 12 del self.__dict__[key] #等同于 self.__dict__.pop(key) 13 14 p=People() 15 print(p.__dict__) 16 # 给__dict__添加属性 17 p["name"]="kelvin" 18 p["age"]=18 19 print(p.__dict__) 20 # 获取__dict__对象属性 21 print(p["name"]) 22 # 删除__dict__对象属性 23 del p["age"] 24 print(p.__dict__)
也就是给对象__dict__增加属性的一种方式。
❤️ 如果你喜欢这篇文章,请点赞支持! 👍 同时欢迎关注我的博客,获取更多精彩内容!
本文来自博客园,作者:佛祖让我来巡山,转载请注明原文链接:https://www.cnblogs.com/sun-10387834/p/10299487.html

浙公网安备 33010602011771号