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

  • 19
    点赞
  • 127
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值