python中__new__和__init__的区别

python中__new__和__init__的区别

__new__方法和__init__方法都是python中的构造方法,其中__new__方法使用较少,__init__方法使用较多。

首先来了解下这两种方法:

1.__new__是在实例创建之前被调用的,用于创建实例,然后返回该实例对象,是个静态方法
2.__init__是当实例对象创建完成后被调用的,用于初始化一个类实例,是个实例方法

由上可知,__new__先被调用,__new__的返回值将传递给__init__方法的第一个参数,然后__init__被调用,给这个实例设置一些参数。

实例:

class A():
    def __new__(cls, *args, **kwargs):
        print('this is A __new__')
        # return super(A, cls).__new__(cls)  # 或者下面这种形式,两种都可
        return object.__new__(cls)

    def __init__(self, title):
        print('this is A __init__')
        self.title = title


a = A('python book')

输出结果:

this is A __new__
this is A __init__

两种方法的区别:

1.__new__至少要有一个参数cls,且必须要有返回值,返回的是实例化出来的实例,有两种return方式:

return super(父类,cls).__new__(cls)
或者
return object.__new__(cls)

2.__init__有一个参数self,就是这个__new__返回的实例,__init__在__new__基础上完成一些其他初始化的动作,__init__不需要有返回值;

注意事项:

1.如果__new__没有返回cls(即当前类)的实例,那么当前类的__init__方法是不会被调用的;

实例:

class A():
    def __new__(cls, *args, **kwargs):
        print('this is A __new__')
        # return super(A, cls).__new__(cls)  

    def __init__(self, title):
        print('this is A __init__')
        self.title = title


a = A('python book')

输出结果:

this is A __new__

2.在定义子类的时候,没有重新定义__new__方法,那么直接继承父类的__new__方法构造该类的实例;

实例:

class A():
    def __new__(cls, *args, **kwargs):
        print('this is A __new__')
        return super(A, cls).__new__(cls)

    def __init__(self, title):
        print('this is A __init__')
        self.title = title


class B(A):
    def __init__(self, title):
        print('this is B __init__')
        self.title = title


b = B('python book')

输出结果:

this is A __new__
this is B __init__

3.在定义子类的时候,重写__new__方法,就不会调用父类的__new__方法;

实例:

class A():
    def __new__(cls, *args, **kwargs):
        print('this is A __new__')
        return super(A, cls).__new__(cls)

    def __init__(self, title):
        print('this is A __init__')
        self.title = title


class B(A):
    def __new__(cls, *args, **kwargs):
        print('this is B __new__')
        return super(A, cls).__new__(cls)  # 或者下面return的方式
        # return object.__new__(cls)

    def __init__(self, title):
        print('this is B __init__')
        self.title = title


b = B('python book')

输出结果:

this is B __new__
this is B __init__

4.如果子类重写__new__方法时,return super(子类,cls),那么会调用父类的__new__方法构造类的实例;

实例:

class A():
    def __new__(cls, *args, **kwargs):
        print('this is A __new__')
        return super(A, cls).__new__(cls)

    def __init__(self, title):
        print('this is A __init__')
        self.title = title


class B(A):
    def __new__(cls, *args, **kwargs):
        print('this is B __new__')
        return super(B, cls).__new__(cls)

    def __init__(self, title):
        print('this is B __init__')
        self.title = title


b = B('python book')

输出结果:

this is B __new__
this is A __new__
this is B __init__

参考来源:
https://www.cnblogs.com/shenxiaolin/p/9307496.html

来源:山和尚

物联沃分享整理
物联沃-IOTWORD物联网 » python中__new__和__init__的区别

发表评论