1.__call__

  这是一个可以把对象变成函数的方法

python 类中的内置方法_实例化python 类中的内置方法_实例化_02

class A():
def __call__(self, *args, **kwargs):
ic("__call__")

# 实例化对象
a = A()
# 对象就像函数一样调用
a("aa") # == a.__call__()

View Code

 2.__new__   和    __init__的爱恨情仇

  new是在   实例化之前执行,必须返回  A()

  init是在    实例化之后,A()会执行此方法

  总结:先new 再init  ;(前提是new有返回值)

  

  a = A()可以理解为:

  a = A类里面的new方法的返回值。

python 类中的内置方法_实例化python 类中的内置方法_实例化_02

class A():
def __init__(self):
ic("init")
def __new__(cls, *args, **kwargs):
ic("new")
return super(A,cls).__new__(cls)

# 实例化对象
a = A()

View Code

 

-----------------------------------------------------------------------------------------------------------------------------------------