类的特殊成员特殊方法__init__;__call__;__del__....
1. 类; 后面加() ===》实例化一个对象,并且执行__init__方法
2. 对象;后面加() ===》执行__call__方法
class Foo: def __init__(self): print("init") def __call__(self, *args, **kwargs): print("call") return 1 r = Foo() # 实例化一个对象,执行__init__方法 r() # 在一个对象后面加括号,执行__call__方法 ret = Foo()() #类Foo()表明实例化一个对象,并且执行__init__方法,后面再()表明执行__call__方法,__call__方法有个返回值1 print(ret)
class Foo: def __init__(self): print("init") def __call__(self, *args, **kwargs): print("call") return 1 def __getitem__(self, item): print(item) def __setitem__(self, key, value): print(key,value) def __delitem__(self, key): print(key) r = Foo() # ===> __init__ r() # ===> __call__ r['k1'] # ===> __getitem__ r['XXX'] = 123 # ===> __setitem__ del r["000"] # ===> __delitem__ r[1:3:1] # ===> __getslice__(2.7)/__getitem__(3.0)(列表) r[1:3] = [11,22,33] # ===> __setslice__(2.7)/__setitem__(3.0)(列表) del r[1:3] # ===> __delslice__(2.7)/__delitem__(3.0)(列表) """ dic = dict(k1=123, k2=456) print(dic['k1']) dic['k1'] = 111 """