__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. git忽略文件并删除git仓库中的文件

    问题描述 不慎在创建.gitignore  文件之前的时候将文件push到了 git仓库,即使之后在.gitignore文件中写入新的过滤规则,这些规则也不会起作用的,git依然会对所有git仓库中的 ...

  2. wpf 制作必输项的*标记

    直接引用帮助文档上的话吧,以免下次忘记! AdornedElementPlaceholder 类 .NET Framework 3.5   其他版本   此主题尚未评级 - 评价此主题   更新:20 ...

  3. 使用spark对hive表中的多列数据判重

    本文处理的场景如下,hive表中的数据,对其中的多列进行判重deduplicate. 1.先解决依赖,spark相关的所有包,pom.xml spark-hive是我们进行hive表spark处理的关 ...

  4. 用FastDFS一步步搭建文件管理系统

    一.FastDFS介绍 FastDFS开源地址:https://github.com/happyfish100 参考:分布式文件系统FastDFS设计原理 参考:FastDFS分布式文件系统 个人封装 ...

  5. Extjs6随笔(终篇)——内容总结

    上个月和Extjs说byebye了,以后大概也没机会用了.之前的博客有点乱,大家看着比较麻烦,所以趁着我还没忘,在这里总结一下♪(^∇^*) 写了个demo,传到git上了,有需要可以自取.Extjs ...

  6. 使用docker 解决一个小问题,你也可能用的到

    以前一直觉得docker是运维用的工具,或者devops 用的工具,一般人应该用不上,直到最近发现docker 还有另外一个妙用,不管是什么语言. 这几天开会网络特别不好,nodejs npm 仓库 ...

  7. centos7安装shipyard没有本地容器及镜像

    目前docker的使用越来越多,自然需要docker的管理工具.现在使用web管理的较多,web基本上实现了跨平台,只需要浏览器,不要额外的管理客户端.web管理主要推荐shipyard和kubern ...

  8. 高性能 Java 缓存库 — Caffeine

    http://www.baeldung.com/java-caching-caffeine 作者:baeldung 译者:oopsguy.com 1.介绍 在本文中,我们来看看 Caffeine - ...

  9. 翻译:MLAPP(2.3节 一些常见的离散分布)

    笔者:尝试翻译MLAPP(Machine Learning: a Probabilistic Perspective)一书,供机器学习的学者参考,如有错误理解之处请指出,不胜感激!(如需转载,请联系本 ...

  10. Mysql存在则更新,没有则新增

    insert ignore 当插入数据时,如出现错误时,如重复数据,将不返回错误,只以警告形式返回. insert ignore into table(col1,col2) values ('val1 ...