Python中__new__和__init__区别
__new__:创建对象时调用,会返回当前对象的一个实例
__init__:创建完对象后调用,对当前对象的一些实例初始化,无返回值
1、在类中,如果__new__和__init__同时存在,会优先调用__new__
>>> class Data(object):
... def __new__(self):
... print "new"
... def __init__(self):
... print "init"
...
>>> data = Data()
new
2、__new__方法会返回所构造的对象,__init__则不会。__init__无返回值。
>>> class Data(object):
... def __init__(cls):
... cls.x = 2
... print "init"
... return cls
...
>>> data = Data()
init
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() should return None, not 'Data'
>>> class Data(object):
... def __new__(cls):
... print "new"
... cls.x = 1
... return cls
... def __init__(self):
... print "init"
...
>>> data = Data()
new
>>> data.x =1
>>> data.x
1
If __new__() returns an instance of cls, then the new instance’s __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to __new__().
如果__new__返回一个对象的实例,会隐式调用__init__
If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.
如果__new__不返回一个对象的实例,__init__不会被调用
<class '__main__.B'>
>>> class A(object):
... def __new__(Class):
... object = super(A,Class).__new__(Class)
... print "in New"
... return object
... def __init__(self):
... print "in init"
...
>>> A()
in New
in init
<__main__.A object at 0x7fa8bc622d90>
>>> class A(object):
... def __new__(cls):
... print "in New"
... return cls
... def __init__(self):
... print "in init"
...
>>> a = A()
in New
object.__init__(self[, ...])
Called when the instance is created. The arguments are those passed to the class constructor expression. If a base class has an __init__() method, the derived class’s __init__() method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: BaseClass.__init__(self, [args...]). As a special constraint on constructors, no value may be returned; doing so will cause a TypeError to be raised at runtime.
在对象的实例创建完成后调用。参数被传给类的构造函数。如果基类有__init__方法,子类必须显示调用基类的__init__。
没有返回值,否则会再引发TypeError错误。
http://www.cnblogs.com/itaceo-o/p/3300289.html
Python中__new__和__init__区别的更多相关文章
- Python中__new__和__init__的区别与联系
__new__ 负责对象的创建而 __init__ 负责对象的初始化. __new__:创建对象时调用,会返回当前对象的一个实例 __init__:创建完对象后调用,对当前对象的一些实例初始化,无返回 ...
- Python中__new__与__init__介绍
在python2.x中,从object继承得来的类称为新式类(如class A(object))不从object继承得来的类称为经典类(如class A()) 新式类跟经典类的差别主要是以下几点: 1 ...
- python中 __new__和__init__
python这两个函数和类的实例化有关. __init__是实例化完成之后调用的,会对生成的对象实例做一些修饰 __new__是python新类型才有的,它更像是c/c++里面的构造函数,因为这个函数 ...
- Python 中__new__()和__init__()的区别
转自: https://blog.csdn.net/weixin_37579123/article/details/89515577 __new__方法:类级别的方法 特性: 1.是在类准备将自身实例 ...
- 【python】__new__和__init__区别
原文:http://blog.csdn.net/cnmilan/article/details/8849680 __new__:创建对象时调用,返回当前对象的一个实例__init__:创建完对象后调用 ...
- Python中__repr__和__str__区别
Python中__repr__和__str__区别 看下面的例子就明白了 class Test(object): def __init__(self, value='hello, world!'): ...
- python中self与__init__怎么解释能让小白弄懂?
python中self与__init__怎么解释能让小白弄懂? 这个问题其实没那么简单. 只说一下自己的理解. python 里所有的 object 都有三个属性, 标识(identity), 类型( ...
- python中// 和/有什么区别
python中// 和/有什么区别 通常C/C++中,"/ " 算术运算符的计算结果是根据参与运算的两边的数据决定的,比如: 6 / 3 = 2 ; 6,3都是整数,那么结果也就是 ...
- [py]python中__new__作用
元类metaclass 使劲搞,但是没搞清楚__new__的作用 了解Python元类 Python进阶:一步步理解Python中的元类metaclass Python中的__new__和__init ...
随机推荐
- ZOJ2965 Accurately Say "CocaCola"! 线性扫描
Accurately Say "CocaCola"! 范围找到:1--700左右,然后打表就ok了 #include<cstdio> #include<cstdl ...
- HDU 5934 强联通分量
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 使用MVVM减少控制器代码实战(减少56%)
减少比例= (360(原来的行数)-159(瘦身后的行数))/360 = 56% 父类 MVC 和MVVM 前后基本不动 父类主要完成如下三个功能: 1)功能:MJRefrsh +上拉下拉没有更多数据 ...
- Lavarel artisan 命令
[alex@iZ25c5aeyiiZ yiqizou3.0]# php artisan list Laravel Framework version Usage: command [options] ...
- 简述static关键字、void与void *(void指针)、函数指针
static关键字1.修饰局部变量,延长局部变量的生命周期.使变量成为静态局部变量,在编译时就为变量分配内存,直到程序退出才释放存储单元.2.修饰全局变量,限制全局变量的使用范围为本文件中.全局变量默 ...
- WPF 只允许打开一个实例
我们有时候只希望我们的程序只打开一个实例,也就是我们的软件只有一次被打开. 那么我们可以通过一个办法知道,在这个软件打开前是不是打开过一个,还没关闭.也就是是否存在另一个程序在运行. 下面是一个简单方 ...
- CloneZilla + Parkomat 实现网络备份
I. 准备 1. 下载 & 烧录 CloneZilla Live 前往 http://clonezilla.org/downloads.php 下载CloneZilla Live,有四个版本, ...
- Windows删除文件时找不到该项目
当在Windows删除文件时出现找不到该项目或者显示该文件不在磁盘中,可以尝试以下方法: 在要删除文件的同级目录下 新建一文本文档,将下列代码复制到文档中,将文档保存为后缀名为.bat的文档(名字随意 ...
- Python 使用正则表达式
当使用正则表达式时,re 模块内部会干两件事情 1.编译正则表达式,如其字串本身不合法,报错 2.用编译后的正则表达式去匹配字符串 re_telephone = re.compile(r'^(\d{3 ...
- highcharts框架使用总结
Highcharts官网地址:https://www.hcharts.cn/products/highcharts首先需要引入jQuery框架,然后包含Highcharts框架需要使用到的js文件,最 ...