class Foo: def __init__(self): pass def __call__(self, *args, **kwargs): pass def __getitem__(self, item): print(item) pass def __setitem__(self, key, value): print(key,value) pass def __delitem__(self, key): pass # 调用__int__方法 Foo() # 调用__call__方法 obj = Foo() obj() #调用__getitem__方法 obj = Foo() obj[‘k1‘] #调用__setitem__方法 obj = Foo() obj[‘k1‘] = 123 #__delitem__ obj = Foo() del obj[‘k1‘]
时间: 2024-10-12 23:06:52