class Foo:
def __init__(self,name):
self.name = name
def __getitem__(self,item):
print('getitem')
print(item)
return self.__dict__.get[item]
def __setitem__(self,key,value):
print('setitem,,,,,',key,value)
self.__dict__[key] = value
print(key,value)
def __delitem__(self, key):
print('delitem,,,,')
self.__dict__.pop(key)
print(key)
obj = Foo('egon')
print(obj.name)
obj['name'] = 'alex'
print(obj.name)
print(obj.__dict__)
del obj['name']
输出:
egon
setitem,,,,, name alex
name alex
alex
{'name': 'alex'}
delitem,,,,
name
标签:__,obj,name,python,self,setitem,key,print
From: https://www.cnblogs.com/heris/p/16817170.html