__getitem__ __setitem__ __delitem__
# __getitem__ __setitem__ __delitem__ # 只适用于类似操作字典的操作才会触发 class Foo: def __getitem__(self, item): print('调用了__getitem__') return self.__dict__[item] def __setitem__(self, key, value): self.__dict__[key] = value print('调用了__setitem__') def __delitem__(self, key): self.__dict__.pop(key) print('调用了__delitem__') f1 = Foo() f1['name'] = 'alex' # 调用了__setitem__ f1['age'] = 18 # 调用了__setitem__ f1['age'] # 调用了__getitem__ print(f1.__dict__) # {'name': 'alex', 'age': 18} del f1['name'] # 调用了__delitem__