class Foo:
def __init__(self,name):
self.name = name
def __getitem__(self, item): # 获取时触发
print('getitem...')
return self.__dict__.get(item)
def __setitem__(self, key, value): # 新增或修改时触发
print('setitem...')
self.__dict__[key]=value
def __delitem__(self, key): # 删除时触发
print('delitem...')
# self.__dict__.pop(key)
del self.__dict__[key]
obj = Foo('jjj')
obj['name'] # 触发 __getitem__
obj['name'] = '小江' # 触发 __setitem__
print(obj['name'])
del obj['name'] # 触发 __delitem__
print(obj.__dict__)