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

测试1:
>>>
class A(object):
def __init__(self):
print("in init")
def
__new__(self):
print("in new")

>>> A()
in
new

测试2:
class A(object):
def __new__(Class):

Object = super(A, Class).__new__(Class)
print "in New"

return Object
def __init__(self):
print "in init"

class
B(A):
def __init__(self):
print "in B's
init"

B()
>>>
in New
in B's
init

对于New来说:
Called to create a new instance of class cls. __new__()
is a static method (special-cased so you need not declare it as such) that takes
the class of which an instance was requested as its first argument. The
remaining arguments are those passed to the object constructor expression (the
call to the class). The return value of __new__() should be the new object
instance (usually an instance ofcls).

Typical implementations create a
new instance of the class by invoking the superclass’s __new__() method using
super(currentclass, cls).__new__(cls[, ...]) with appropriate arguments and then
modifying the newly-created instance as necessary before returning
it.

这也是上面的例子2

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__().

If __new__() does not return an instance of cls,
then the new instance’s __init__() method will not be invoked.

__new__()
is intended mainly to allow subclasses of immutable types (like int, str, or
tuple) to customize instance creation. It is also commonly overridden in custom
metaclasses in order to customize class creation.

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.  

【python】__new__和__init__区别的更多相关文章

  1. python __new__以及__init__

    @[深入Python]__new__和__init__ 1 2 3 4 5 6 7 8 class A(object):     def __init__(self):         print & ...

  2. Python中__new__和__init__区别

    __new__:创建对象时调用,会返回当前对象的一个实例 __init__:创建完对象后调用,对当前对象的一些实例初始化,无返回值 1.在类中,如果__new__和__init__同时存在,会优先调用 ...

  3. 飘逸的python - __new__、__init__、__call__傻傻分不清

    __new__: 对象的创建,是一个静态方法.第一个參数是cls.(想想也是,不可能是self,对象还没创建,哪来的self) __init__ : 对象的初始化, 是一个实例方法,第一个參数是sel ...

  4. 1.(python)__new__与__init__

    1.来比较一下__new__与__init__: (1)__new__在初始化实例前调用,__init__在初始化实例之后调用,用来初始化实例的一些属性或者做一些初始操作 # -*- coding: ...

  5. python __new__和__init__的区别

    http://www.cnblogs.com/tuzkee/p/3540293.html 继承自object的新式类才有__new__ __new__至少要有一个参数cls,代表要实例化的类,此参数在 ...

  6. [深入Python]__new__和__init__

    class A(object): def __init__(self): print "init" def __new__(cls,*args, **kwargs): print ...

  7. python __new__和__init__

    转载:http://www.cnblogs.com/tuzkee/p/3540293.html 1 2 3 4 5 6 7 8 class A(object):     def __init__(se ...

  8. Python基础(十) __init__与__new__区别

    __init__与__new__区别: __init__在python,其实是,在实例化之后执行的,用来初始化一些属性,相当于构造函数,但是又不一样 细心一些,通过参数会有所发现,其实__init__ ...

  9. Python 中的__new__和__init__的区别

    [同] 二者均是Python面向对象语言中的函数,__new__比较少用,__init__则用的比较多. [异] __new__是在实例创建之前被调用的,因为它的任务就是创建实例然后返回该实例对象,是 ...

随机推荐

  1. 解决Android5.0以下Dialog引起的内存泄漏

    最近项目开发中,开发人员和测试人员均反应在android5.0以下手机上LeakCanary频繁监控到内存泄漏,如下图所示,但凡用到Dialog或DialogFragment地方均出现了内存泄漏. 如 ...

  2. c#使用GDI+简单绘图

    private void button2_Click(object sender, EventArgs e) { Bitmap image = new Bitmap(200, 200); Graphi ...

  3. BaseServlet,让一个servlet处理多个请求

    BaseServlet 第一次学习servlet的时候是跟着传智播客免费的教学视频,其中崔希凡讲的是我学过自认讲的最好的一位,BaseServlet也是跟着他写过一次,当时很多东西不能理解,后来慢慢发 ...

  4. UWP 手绘视频创作工具技术分享系列 - 位图的绘制

    前面我们针对 SVG 的解析和绘制做了介绍,SVG 是图片的一种形式,而另一种很重要的图片是:位图,包括 png.jpeg.bmp 等格式.位图的基本规则是,组成的基本元素是像素点,由宽度 * 高度个 ...

  5. mysql在cmd命令下执行数据库操作

    windows+r 运行cmd命令,执行以下操作! 当mysql 数据库文件相对于来说比较大的时候,这个时候你可能在正常环境下的mysql中是导入不进去的,因为mysql数据库本身就有默认的导入文件大 ...

  6. PHP之回调函数传参(解决eval函数拼接对象参数的问题)

    在使用Smarty时,定义了一个统一调用控制器的函数,如下: function C($name, $method){//控制器的名称和其中方法的名称 require_once "contro ...

  7. 解决Nginx+Tomcat时ContextPath不同的问题

    1        问题描述 项目前端模板使用Thymeleaf,在对各种URL进行格式化输出时,都使用@{uri}代码.它会自动读取项目部署的虚拟路径,添加到URI的前端输出. 真实测试和生产环境中, ...

  8. 如何才能通俗易懂的解释javascript里面的"闭包"?

    看了知乎上的话题 如何才能通俗易懂的解释javascript里面的‘闭包’?,受到一些启发,因此结合实例将回答中几个精要的答案做一个简单的分析以便加深理解. 1. "闭包就是跨作用域访问变量 ...

  9. python的re正则表达

    正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达式.常规表示法(英语:Regular Expression,在代码中常简写为regex.regexp或RE),是计算机科学的一个概念.正则表 ...

  10. Arch Linux之pacman调用axel多线程加速下载

    转载自 奶牛博客 本来感觉Arch Linux用个国内的源就很给力了,可是到了学校移动的cmcc-edu超级不稳定,而且单线程速度就二三十k,无奈,开多线程下载.在Ubuntu下面可以用apt-fas ...