类的特殊成员方法

class Foo():

  def __init__(self):

    print('init')

  def __call__(self,*args,**kwargs)

    print('call')

  def __getitem__(self,item):

    print(item)

  def __setitem__(self,key,value)

    print(key,value)

  def __delitem__(self,item)

    print(key)

r = Foo()  # 执行__init__ 方法

r()            # 执行 __call__方法

r[‘keyee’]            # 执行 __getitem__方法

r['key1'] = 123    # 执行 __setitem__方法

del r['key2']        # 执行 __delitem__方法

原文地址:https://www.cnblogs.com/huangguabushihaogua/p/10029477.html