python中__new__的用法_python基础教程之__new__的用法介绍

今天介绍下__new__的用法:

__new__是新式类中出现,是一种静态方法。在python官方文档中,介绍了在那些情况下可以使用:

Use __new__ when you need to control the creation of a new instance. Use __init__ when you need to control initialization of a new instance.

__new__ is the first step of instance creation. It's called first, and is responsible for returning a new instance of your class. In contrast, __init__ doesn't return anything; it's only responsible for initializing the instance after it's been created.

In general, you shouldn't need to override __new__ unless you're subclassing an immutable type like str, int, unicode or tuple.

从上面英文的意思,可以知道:

需要控制实例的创建的时候,使用__new__;在类创建过程中,最先调用并返回类实例;在实例已经被创建出来的时候,当需要实例初始化的时候,使用__init__;一般来说,不建议重写__new__,除非是在构建str,int,unincode或者tuple不可变类型的子类的时候

从中可以看出,__new__负责对象的创建,__init__负责对象的初始化,因此__new__的过程在__init__之前。

__new__的定义:

def __new__(cls,*args,**kwargs):

pass

参数说明:cls指当前正在实例化的类

根据__new__定义我们可以设计以下几种实验场景:

1)返回该类本身创建实例

class clsA(object):

def __new__(cls, *args, **kwargs):

print('A')

print(cls)

print(clsA)

return object.__new__(cls,*args,**kwargs)

def __init__(self):

print('A.__init__')

print(clsA())

输出结果是:

A

A.__init__

从第一个例子中,我们可以看出,参数cls和clsA等价

2)返回另一个类的实例

class clsB(object):

def __new__(cls, *args, **kwargs):

return clsA.__new__(clsA,*args,**kwargs)

def __init__(self):

print('clsB.__init__')

print(clsB())

输出结果是:

A

从第二个例子中看出,打印出了clsA中__new__输出的东西,并没有执行clsB.__init__;这是因为clsB没有成功的创建clsB的实例,那么clsB.__init__没有可接收的self对象,当然无法执行了。

3)__new__没有return的情况

class clsC(object):

def __new__(cls, *args, **kwargs):

print('clsC.__new__')

def __init__(self):

print('clsC.__init__')

print(clsC())

输出结果是:

clsB.__new__

None

从第三个例子,可以看出clsC没有返回对象时,__init__是不执行的,并且打印对象的信息也是None,因为没有创建实例对象。

最后说下,__new__和__init__不是必须写的,没有它们的会直接继承Object的。

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值