标签:ret val int name 修改 key def self init
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__)
标签:ret val int name 修改 key def self init
原文地址:https://www.cnblogs.com/jt925/p/10502672.html