__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. 一步使你的asp.net网站在手机浏览器上全屏显示

    首先要加入下面的代码: <meta name="viewport" content="width=device-width, initial-scale=1.0, ...

  2. asp.net(C#)利用QRCode生成二维码

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="QRCode.aspx.cs&q ...

  3. mySQL使用实践

    1.虚拟机安装mySQL 服务器, 宿主机分别使用navicat工具和java代码 访问mySQL,组网图如下: 2. 查看mySQL的服务器状态,如下: 3. 服务器上查看数据库和数据表内容如下: ...

  4. cmd启动,重启,停止IIS命令

    直接使用CMD我们可以操作很多事情,比如启动IIS,重启IIS,停止IIS 重启IIS服务器,开始->运行->cmd (以下列出相关操作命令): iisreset /RESTART 停止后 ...

  5. [ZJOI2005]九数码游戏

    [ZJOI2005]九数码游戏 题目描述 输入输出格式 输入格式: 输入文件中包含三行三列九个数,同行的相邻两数用空格隔开,表示初始状态每个方格上的数字.初始状态不会是目标状态. 输出格式: 如果目标 ...

  6. 2017年十大奇葩画风的H5页面案例,原来脑洞可以这样大

    每个人都是视觉动物,画面精美.体验奇特的H5,用户在内心一般都会满分打出,毫不吝啬,同时也毫不犹豫分享,因为此时的分享不掉价儿~ 今天给大家准备了十支H5,画风超级奇特,非常值得一看所有案例均可在19 ...

  7. IIS ApplicationPoolIdentity(配置IIS讀寫網站文件)

    原创地址:http://www.cnblogs.com/jfzhu/p/4067297.html 转载请注明出处 从IIS 7.5开始,Application Pool Identity的Built- ...

  8. Django——模板层(template)(模板语法、自定义模板过滤器及标签、模板继承)

    前言:当我们想在页面上给客户端返回一个当前时间,一些初学者可能会很自然的想到用占位符,字符串拼接来达到我们想要的效果,但是这样做会有一个问题,HTML被直接硬编码在 Python代码之中. 1 2 3 ...

  9. 【ASP.NET MVC 学习笔记】- 03 Razor语法

    本文参考:http://www.cnblogs.com/willick/p/3224144.html 1.Razor语句以@开头. 2.每个View都有自己的Model属性,可通过@Model调用.语 ...

  10. spark在yarn-cluster模式,错误查找方法

    yarn logs -applicationId application_xxxx_xxx 可选(">exception")