class A():标签:__,index,obj,python,self,getitem,def From: https://blog.51cto.com/haoyonghui/6020118
def __init__(self):
self.name = {"key": "aaa"}
def __getitem__(self, item):
return self.name.get(item)
obj = A()
print(obj["key"])
实现迭代
class B():
def __init__(self, a_list):
self.a = a_list
self.other = "hello, world"
def __getitem__(self, index):
return self.a[index]
obj = B(["dog","cat","fish"])
for a in obj:
print(a)