class A():
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)