OBJ[XXX] = "XX" This action triggers the __setitem__ method of the object.
del obj["xxx"] This action triggers the __delitem__ method of the object.
obj["XXX"] triggers the __getitem__ method of the object.
Class Foo:
def __init__ (self,name):
Self.name=name
def __getitem__ (self, item):
Print (Self.__dict__[item])
def __setitem__ (self, Key, value):
Self.__dict__[key]=value
def __delitem__ (self, key):
Print (' del Obj[key], I execute ')
Self.__dict__.pop (Key)
def __delattr__ (self, item):
Print (' del obj.key, I execute ')
Self.__dict__.pop (item)
F1=foo (' SB ')
f1[' age ']=18
f1[' Age1 ']=19
Del F1.age1
Del f1[' age ']
f1[' name ']= ' AAA '
Print (f1.__dict__)
This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1918007
8.python Face Object part.7 (__setitem__,__getitem,__delitem__)