Python中__new__和__init__区别
__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
转载于:https://www.cnblogs.com/gsblog/p/3368304.html
Python中__new__和__init__区别相关推荐
- 简述面向对象中__new__和__init__区别,这道题朝简单!
简述面向对象中__new__和__init__区别 使用Python写过面向对象的代码的同学,可能对 __init__ 方法已经非常熟悉了,__init__ 方法通常用在初始化一个类实例的时候.例如: ...
- Python中__new__和__init__的区别与联系
__new__和__init__的区别主要表现在:它自身的区别及在Python中新式类和老式类的定义. __new__ 负责对象的创建而 __init__ 负责对象的初始化. __new__:创建对象 ...
- Python 中__new__()和__init__()的区别
转自: https://blog.csdn.net/weixin_37579123/article/details/89515577 __new__方法:类级别的方法 特性: 1.是在类准备将自身实例 ...
- 列举至少五个python内置函数和使用方法_Python内置函数 next的具体使用方法 Python中seek和next区别...
python列表本来没有next方法,为什么用iter函数生...对list用__dir__()发现没有next方法,但是用iter()生成迭代器,对该迭代next是进行迭代的方法,只有迭代器和生成器 ...
- Python中常见的__init__.py是什么意思?详解Python import的方式和原理
Python中常见的__init__.py是什么意思?详解Python import的方式和原理 1 什么是模块化编程? 2 __init__.py文件的作用 3 Python如何import第三方库 ...
- python中arg,*arg,**kwarg区别
python中arg,*arg,**kwarg区别 1. *args(*的作用很重要) 首先要搞清楚*的意思和作用,有助于理解arg,*arg,**kwarg. (*是arg的前半部分,arg是后半部 ...
- Python中is和==的区别以及is None
0 前言 Python中有很多运算符,今天我们就来讲讲is和==两种运算符在应用上的本质区别是什么. 在讲is和==这两种运算符区别之前,首先要知道Python中对象包含的三个基本要素,分别是:id( ...
- Python中“is”和“==”的区别
Python有两个用于相等比较的运算符,"is"和"=="(等于).在这篇文章中,我将教你们两者之间的区别,以及通过几个简单地例子说明什么时候使用它们. 当我还 ...
- python中绝对路径的区别,理解Python中的绝对路径和相对路径
本文介绍了Python中的绝对路劲和相对路径,分享给大家,也给自己留个笔记 1.绝对路径 os.path.abspath("文件名"): 显示的是一个文件的绝对路劲 eg: > ...
最新文章
- (C++)变长数组vector的常见用法
- 《Drupal实战》——1.9 小结
- javascript简单的四则运算
- 使用MCI方法操作声音文件
- Android涂鸦技术及刮刮乐示例分析
- 深入理解 JVM Class文件格式(一)
- Vondrak滤波原理详解及Matlab实现
- 判断元素是否存在(信息学奥赛一本通-T1211)
- 出现问题protected:void __thiscall CTalkCDlg::OnIdok(void)(?OnIdok@CTalkDlg@@IAEXXZ)?
- [软件]Beyond Compare
- SpringApplication#run⽅法第5步,打印banner(四)
- php5 安装imagick,为php安装imagick拓展与image magick图像处理库
- 微信公众号之微信认证
- 【GitHub】README.md文件中 markdown语法 插入超链接
- JobTracker作业启动过程分析
- http免费代理-2020-04-07
- 消息队列MQ常见面试题
- strstr(str1,str2) 函数
- 四种方法搞定电脑数据备份
- 过年回家和小朋友玩什么
热门文章
- 做一个更好的A牌 从《Artifact》2.0看Valve的设计思路
- pyecharts第六节、水球图
- Spring框架中有哪些不同类型的事件
- 在11g中alter system set recyclebin = off 报错 recyclebin 参数在11g和10g中的区别
- Oracle truncate table 与 delete tabel的区别(转)
- 【Oracle】查看死锁与解除死锁
- C# SQLite事务操作方法分析
- 解决UIScrollView把uitableviewcell的点击事件屏蔽
- shell--printf
- SpringMVC一例 是否需要重定向