__new__、__init__、__del__三个方法用于实例的创建和销毁,在使用python的类中,我们最常用的是__init__方法,通常称为构造方法,__new__方法几乎不会使用,这篇文章是基于Python3.6为基础来做实验,主要谈一谈python的__new__和__init__,__init__ 通常称为构造方法,但其实它是个“初始化方法”,真正的构造方法是 __new__, __init__既然是初始化方法,那么肯定是对对象的初始化,也就是存在一个被初始化的对象
来看第一个例子:
class inch(): def __init__(self): print("__init__") def __new__(cls): print("__new__ ") print("__new__ ")__new__None
因为我们没有从__new__返回任何结果,__init__这里不会调用。如果init被调用,我们就会看到我们在init中的打印语句。
再看第二个例子:
class inch(): def __new__(cls): print("__new__ ") print(super(inch, cls).__new__(cls)) return super(inch, cls).__new__(cls) def __init__(self): print("__init__")print(inch())__new__<__main__.inch object at 0x002BF170>__init__<__main__.inch object at 0x002BF170>
用于构建实例的是特殊方法 __new__:是个类方法(不需要使用@classmethod),调用 __init__ 方法时要传入实例,__new__必须返回一个实例。返回的实例会作为第一个参数(即 self)传给 __init__ 方法。注意:__init__不允许返回任何值
再看第三个列子:
class inch(float): def __new__(cls, args=0.0): cls.new = float.__new__(cls,args*0.0254) return cls.new def __init__(self,a): print("__init__") print(a)object1=inch(3)print(object1.new)__init__30.07619999999999999
无论我们给超类的__new__传递的是哪个类,它都会创建该类的一个实例,如何我们想要创建一个inch的实例,所以,inch类必须作为第一个参数传递给float.__new__。在类inch的内部,cls指的是inch类,因此,我们需要将cls作为第一个参数传递给对象。
第四个例子:
class B(float): passclass inch(float): def __new__(cls, args=0.0): B.new = float.__new__(B,args*0.0254) return B.new def __init__(self,a): print("__init__") print(a)object1=inch(3)print(type(object))print(object1.new)<class '__main__.B'>0.0761999999999999
最后:__new__是用来创建一个实例的,从 object 类继承的已经很完善。所以我们基本上不需要自己编写 __new__ 方法
第四个例子,一个__new__的应用,著名的单例模式(:python 中 None
对象就是单例):
class Singleton: _instance = None def __new__(cls, *args, **kwargs): if not isinstance(cls._instance, cls): cls._instance = object.__new__(cls, *args, **kwargs) return cls._instancea=Singleton()b=Singleton()print(a is b)True
__del__:在需要销毁实例的时候,python解释器会调用__del__方法。Cpython中垃圾回收的主要算法是引用计数,每个对象会统计有多少引用指向自己。当引用计数归零时,对象立即就被销毁