参考链接
https://blog.csdn.net/qq_24805141/article/details/81411775
https://zhuanlan.zhihu.com/p/27661382
https://www.cnblogs.com/Aiyuqianer/p/14091062.html
__getitem__是一种特殊方法(魔术方法),能让对象具有迭代功能。
class DataBase:
'''Python 3 中的类'''
def __init__(self, id, address):
'''初始化方法'''
self.id = id
self.address = address
def __getitem__(self, key):
print(self.__dict__)
return self.__dict__.get(key, "100")
data = DataBase(1, "192.168.2.11")
print(data["hi"])
print(data["id"])
self.dict :{‘id’:1, ‘address’: "“192.168.2.11”'}
首先判断将key值"hi"是否存在,不存在 返回100
判断key值“id”是否存在,存在即返回对应value,也就是1