Python_中__init__和__new__的异同
1:__new__:它是创建对象时调用,会返回当前对象的一个实例;
__init__:它是创建对象后调用,对当前对象的一些实例初始化,无返回值
代码示例:
>>> class Data(object):
def __init__(cls):
cls.x = 2
print "init"
return cls ###在init中不可用 >>> data = Data()
init Traceback (most recent call last):
File "<pyshell#100>", line 1, in <module>
data = Data()
TypeError: __init__() should return None, not 'Data' >>> class Data(object):
def __new__(cls):
cls.x = 3
print "new"
return cls >>> data = Data()
new
>>> data.x
3
由上可见,__new__方法会返回所构造的对象,__init__则不会,__init__无返回值
2:再类中,若__new__和__init__同时存在时,先调用__new__
代码示例:
>>> class Data(object):
def __new__(self):
print "new"
def __init__(self):
print "init" >>> data = Data()
new
3: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 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 0x0215CD70>
>>> class B(object):
def __new__(cls):
print "in new"
return cls
def __init__(self):
print "in init" >>> b=B()
in new
Python_中__init__和__new__的异同的更多相关文章
- Python中__init__和__new__的区别详解
__init__ 方法是什么? 使用Python写过面向对象的代码的同学,可能对 __init__ 方法已经非常熟悉了,__init__ 方法通常用在初始化一个类实例的时候.例如: # -*- cod ...
- python中__init__()、__new__()、__call__()、__del__()几个魔法方法的用法
关于__new__()的用法参考: http://www.myhack58.com/Article/68/2014/48183.htm 正文: 一.__new__()的用法: __new__()是在新 ...
- python中__init__()、__new__()、__call__()、__del__()用法
关于__new__()的用法参考: http://www.myhack58.com/Article/68/2014/48183.htm 正文: 一.__new__()的用法: __new__()是在新 ...
- python中__init__和__new__的区别
参考:https://my.oschina.net/liuyuantao/blog/747164 python中__metaclass的详解 参考:https://www.cnblogs.com/ia ...
- __init__和__new__的异同
实例化类的流程: 1.p = Person(name, age)2.首先执行使用name和age参数来执行Person类的__new__方法,这个__new__方法会 返回Person类的一个实例(p ...
- Python中的__init__和__new__介绍
介绍 首先我们要知道在面向对象编程中,实例化基本遵循创建实例对象.初始化实例对象.最后返回实例对象这么一个过程. Python 中的 __new__ 方法负责创建一个实例对象,__init__ 方法负 ...
- python中的__init__ 、__new__、__call__小结
这篇文章主要介绍了python中的__init__ .__new__.__call__小结,需要的朋友可以参考下 1.__new__(cls, *args, **kwargs) 创建对象时调用,返回 ...
- 详解python中的__init__与__new__方法
一.__init__和__new__方法执行的顺序? 在面向对象中介绍了关于对象创建的过程,我们知道__new__方法先于__init__方法执行. 二.__new__方法是什么? 首先,我们先来看下 ...
- Python中的__init__和__new__
一.__init__ 方法是什么? 使用Python写过面向对象的代码的同学,可能对 __init__ 方法已经非常熟悉了,__init__ 方法通常用在初始化一个类实例的时候.例如: # -*- c ...
随机推荐
- 强化基础 Action ac = (System.Action)delegate() { Console.WriteLine("123456"); }; ac(); 委托间 也是 可以相互转换的
委托间 也是 可以相互转换的
- 对OpenCV中Haar特征CvHaarClassifierCascade等结构理解
首先说一下这个级联分类器,OpenCV中级联分类器是根据VJ 04年的那篇论文(Robust Real-Time Face Detection)编写的,查看那篇论文,知道构建分类器的步骤如下: 1.根 ...
- JSTL简单介绍
1.JSTL简单介绍: JSTL(JSP Standard Tag Library.JSP标准标签库)是一个不断完好的开放源码的JSP标签库.其提供两组标签,一组使用 EL(Expression La ...
- java 定义一个同步map内存去重法
实例:
- public,protected,private,static,final的区别(转载)
1.类 (1)在java中有public.protected.private三种显示的修饰符用于控制可见性,package不是显示的修饰符,它是隐含的,即如果在类.变量等前没加显示的可见性修饰符,那它 ...
- 搭建mysql主从集群的步骤
前提条件是:须要在linux上安装4个mysql数据库,都须要配置完对应的信息. 须要搭建: mysql 01: 主数据库 master mysql 02 : ...
- # Playables API(翻译)
The Playables API provides a way to create tools, effects or other gameplay mechanisms by organi ...
- [Phoenix] 八、动态列
摘要: 传统关系型数据库的动态列实现只能依赖逻辑层的设计实现,而Phoenix是HBase上的SQL层,借助HBase特性实现的动态列功能,具有高度的灵活性,告别业务逻辑层的复杂设计. 一.概要 动态 ...
- android DownloadManager.getInputStream返回null的一种情况
将下载操作的代码放到一个新的子线程中来执行.
- 【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...