__setitem__和__getitem和__delitem__
目录一、__setitem__二、__getitem__三、__delitem__与__delattr__
class Foo: def __init__(self, name): self.name = name def __getitem__(self, item): print('getitem执行', self.__dict__[item]) def __setitem__(self, key, value): print('setitem执行') self.__dict__[key] = value def __delitem__(self, key): print('del obj[key]时,delitem执行') self.__dict__.pop(key) def __delattr__(self, item): print('del obj.key时,delattr执行') self.__dict__.pop(item) f1 = Foo('sb')一、__setitem__
中括号赋值时触发
f1['age'] = 18 f1['age1'] = 19 setitem执行 setitem执行二、__getitem__
中括号取值时触发
f1['age'] getitem执行 18 f1['name'] = 'tank' setitem执行三、__delitem__与__delattr__
__delitem__:中括号删除时触发
__delattr__:.删除时触发