__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__区别的更多相关文章

  1. Python中__new__和__init__的区别与联系

    __new__ 负责对象的创建而 __init__ 负责对象的初始化. __new__:创建对象时调用,会返回当前对象的一个实例 __init__:创建完对象后调用,对当前对象的一些实例初始化,无返回 ...

  2. Python中__new__与__init__介绍

    在python2.x中,从object继承得来的类称为新式类(如class A(object))不从object继承得来的类称为经典类(如class A()) 新式类跟经典类的差别主要是以下几点: 1 ...

  3. python中 __new__和__init__

    python这两个函数和类的实例化有关. __init__是实例化完成之后调用的,会对生成的对象实例做一些修饰 __new__是python新类型才有的,它更像是c/c++里面的构造函数,因为这个函数 ...

  4. Python 中__new__()和__init__()的区别

    转自: https://blog.csdn.net/weixin_37579123/article/details/89515577 __new__方法:类级别的方法 特性: 1.是在类准备将自身实例 ...

  5. 【python】__new__和__init__区别

    原文:http://blog.csdn.net/cnmilan/article/details/8849680 __new__:创建对象时调用,返回当前对象的一个实例__init__:创建完对象后调用 ...

  6. Python中__repr__和__str__区别

    Python中__repr__和__str__区别 看下面的例子就明白了 class Test(object): def __init__(self, value='hello, world!'): ...

  7. python中self与__init__怎么解释能让小白弄懂?

    python中self与__init__怎么解释能让小白弄懂? 这个问题其实没那么简单. 只说一下自己的理解. python 里所有的 object 都有三个属性, 标识(identity), 类型( ...

  8. python中// 和/有什么区别

    python中// 和/有什么区别 通常C/C++中,"/ " 算术运算符的计算结果是根据参与运算的两边的数据决定的,比如: 6 / 3 = 2 ; 6,3都是整数,那么结果也就是 ...

  9. [py]python中__new__作用

    元类metaclass 使劲搞,但是没搞清楚__new__的作用 了解Python元类 Python进阶:一步步理解Python中的元类metaclass Python中的__new__和__init ...

随机推荐

  1. ubuntu环境下lnmp环境搭建(2)之Nginx

    1. ubuntu编译安装nginx http://www.cnblogs.com/zhangjun516/archive/2013/02/03/2890990.html 1. 手动编译安装 Ngin ...

  2. C#-WinForm 串口通信

    //C# 的串口通信,是采用serialPort控件,下面是对serialPort控件(也是串口通信必备信息)的配置如下代码: serialPort1.PortName = commcomboBox1 ...

  3. 学习总结---BGP协议

    一.可以在自治域内使用BGP作为域内协议吗? 为什么?它和OSPF的关键差异是什么? 1.BGP的全称是边界网关协议,用于自治域间的路由传递,它不像OSPF协议,其重点不在于路由的计算,而在于路由的控 ...

  4. Scrapy架构及其组件之间的交互

    最近在学Python,同时也在学如何使用python抓取数据,于是就被我发现了这个非常受欢迎的Python抓取框架Scrapy,下面一起学习下Scrapy的架构,便于更好的使用这个工具. 一.概述 下 ...

  5. C#如何连接wifi和指定IP

    大家好哈,这是我第一次写博客,我也是才大一结束,自学了10多天C#,有不对的欢迎大家指正,最近因为项目的事而被Wifi和IP折磨了很久,后来借用了一下外国人的SimpleWifi.dll 再自己写了一 ...

  6. IIS6、7添加反向代理的步骤

    1.安装requestRouter_amd64.msi和rewrite_x64_zh-CN.msi. 打包下载地址:http://files.cnblogs.com/files/wangwust/ii ...

  7. Web前端性能优化——如何有效提升静态文件的加载速度

    WeTest 导读 此文总结了笔者在Web静态资源方面的一些优化经验. 一.如何优化 用户在访问网页时, 最直观的感受就是页面内容出来的速度,我们要做的优化工作, 也主要是为了这个目标.那么为了提高页 ...

  8. java笔记3(动手动脑)

    1.以下代码为何无法通过编译?哪儿出错了? 原因:已有的Foo()是带一个int型参数的构造方法,不存在无参的构造方法Foo() "构造方法" 当创建一个对象时,它的构造方法会被自 ...

  9. 运行第一个Go Web框架

    GO 语言的web框架很多,相对来说, Beego 框架,入门简单,文档齐全(中文),功能强大,本文以Beego 示例. Beego提供了详细的开发文档:http://beego.me/docs/in ...

  10. Spring容器组建注解@Component和Resouces实现完全注解配置

    @Resource和@Component实现零XML配置 1.@Resource的注解: @Resource是J2EE的注解.意思是说在容器里面找相应的资源.也可以通过name属性指定它name的资源 ...