__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. ZOJ2185 简单分块 找规律

    初步找大概位置,然后找精确位置,算是简单化的分块吧! #include<cstdio> #include<cstdlib> #include<iostream> u ...

  2. java web Servlet 学习笔记 -3 会话管理技术

     Cookie和HttpSession 什么是会话: 用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 每个用户在使用浏览器与服务器进行会话的过 ...

  3. WPF DataGrid自定义样式

    微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师).下面,找到我的小抄造型的网格.它不是100%全面,但它可以让你走得很远,有一些非常有用的技巧和陷阱. ...

  4. SpringMVC拦截器Interceptor

    SpringMVC拦截器(Interceptor)实现对每一个请求处理前后进行相关的业务处理,类似与servlet中的Filter. SpringMVC 中的Interceptor 拦截请求是通过Ha ...

  5. 学习笔记之08试用div做网页(滨院)-小作业

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 【转载】WAI-ARIA无障碍网页应用属性完全展示

    文章转载自 张鑫旭-鑫空间-鑫生活 http://www.zhangxinxu.com/wordpress/ 原文链接:http://www.zhangxinxu.com/wordpress/?p=2 ...

  7. 使用OLAMISDK实现一个语音输入数字进行24点计算的iOS程序

    前言 在目前的软件应用中,输入方式还是以文字输入方式为主,但是语音输入的方式目前应用的越来越广泛.这是一个利用 Olami SDK 编写的一个24点iOS程序,是通过语音进行输入. Olami SDK ...

  8. Bootstrap 禁用滚动条

    Bootstrap中禁用滚动条的方法 逻辑: 当点击弹窗按钮后,js会为body元素添加一个modal-open的类,该类主要内容如下 .modal-open .modal { overflow-x: ...

  9. nginx + tomcat + redis 部署项目,解决session共享问题。

    最近自己搭了一套nginx的环境,集群部署了公司的一个项目,中间解决了session共享的问题.记录如下,以备日后查看. 1.环境 windows10 家庭中文版,jdk 7, tomcat 7.0. ...

  10. 进程,线程,GIL,Python多线程,生产者消费者模型都是什么鬼

    1. 操作系统基本知识,进程,线程 CPU是计算机的核心,承担了所有的计算任务: 操作系统是计算机的管理者,它负责任务的调度.资源的分配和管理,统领整个计算机硬件:那么操作系统是如何进行任务调度的呢? ...