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 ...
随机推荐
- #云栖大会# 移动安全专场——APP加固新方向(演讲速记)
主持人导语: 近些年来,移动APP数量呈现爆炸式的增长,黑产也从原来的PC端转移到了移动端,伴随而来的逆向攻击手段也越来越高明.在解决加固产品容易被脱壳的方案中,代码混淆技术是对抗逆向攻击最有效的方式 ...
- SQL SERVER 根据地图经纬度计算距离函数
前些天客户提出一个这样的要求:一个手机订餐网,查询当前所在位置的5公里范围的酒店,然后客户好去吃饭. 拿到这个请求后,不知道如何下手,静静地想了一下,在酒店的表中增加两个字段,用来存储酒店所在的经度和 ...
- 压缩SQLServer数据库日志的一个存储过程
use master --注意,此存储过程要建在master数据库中 go if exists (select * from dbo.sysobjects where id = object_id(N ...
- 实现LAMP
实现LAMP 1.LAMP工作原理 LAMP是一个强大的Web应用程序平台,其中L是指linux系统:A是指apache也就是http;M一般是mysql/mariadb数据库;P一般是php, pe ...
- 胡小兔的OI日志3 完结版
胡小兔的 OI 日志 3 (2017.9.1 ~ 2017.10.11) 标签: 日记 查看最新 2017-09-02 51nod 1378 夹克老爷的愤怒 | 树形DP 夹克老爷逢三抽一之后,由于采 ...
- 我的three.js学习记录(三)
此次的亮点不是three.js的3d部分,而是通过调用摄像头然后通过摄像头的图像变化进行简单的判断后进行一些操作.上篇中我通过简单的示例分析来学习three.js,这次是通过上一篇的一些代码来与摄像头 ...
- Fatal error in launcher:Unable to create process using '"'
Windows下同时存在Python2和Python3使用pip时系统报错:Fatal error in launcher: Unable to create process using '" ...
- 解题笔记-洛谷-P1010 幂次方
0 题面 题目描述 任何一个正整数都可以用2的幂次方表示.例如 137=2^7+2^3+2^0 同时约定方次用括号来表示,即a^b 可表示为a(b). 由此可知,137可表示为: 2(7)+2(3)+ ...
- .NET下发送邮件遇到问题及解决方案
.NET后台代码利用QQ邮箱服务器发送邮件遇到的问题: "mail from address must be same as authorization user" 首先,看下我的 ...
- Oracle学习笔记之PL/SQL编程
SQL(Structure Query Language)的含义是结构化查询语句,最早由Boyce和Chambedin在1974年提出,称为SEQUEL语言.1976年,IBM公司的Sa ...