【python】__new__和__init__区别
__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__区别的更多相关文章
- python __new__以及__init__
@[深入Python]__new__和__init__ 1 2 3 4 5 6 7 8 class A(object): def __init__(self): print & ...
- Python中__new__和__init__区别
__new__:创建对象时调用,会返回当前对象的一个实例 __init__:创建完对象后调用,对当前对象的一些实例初始化,无返回值 1.在类中,如果__new__和__init__同时存在,会优先调用 ...
- 飘逸的python - __new__、__init__、__call__傻傻分不清
__new__: 对象的创建,是一个静态方法.第一个參数是cls.(想想也是,不可能是self,对象还没创建,哪来的self) __init__ : 对象的初始化, 是一个实例方法,第一个參数是sel ...
- 1.(python)__new__与__init__
1.来比较一下__new__与__init__: (1)__new__在初始化实例前调用,__init__在初始化实例之后调用,用来初始化实例的一些属性或者做一些初始操作 # -*- coding: ...
- python __new__和__init__的区别
http://www.cnblogs.com/tuzkee/p/3540293.html 继承自object的新式类才有__new__ __new__至少要有一个参数cls,代表要实例化的类,此参数在 ...
- [深入Python]__new__和__init__
class A(object): def __init__(self): print "init" def __new__(cls,*args, **kwargs): print ...
- 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 ...
- Python基础(十) __init__与__new__区别
__init__与__new__区别: __init__在python,其实是,在实例化之后执行的,用来初始化一些属性,相当于构造函数,但是又不一样 细心一些,通过参数会有所发现,其实__init__ ...
- Python 中的__new__和__init__的区别
[同] 二者均是Python面向对象语言中的函数,__new__比较少用,__init__则用的比较多. [异] __new__是在实例创建之前被调用的,因为它的任务就是创建实例然后返回该实例对象,是 ...
随机推荐
- 将图标LOGO之类的图形图像转换成字体调用方法大全
借鉴百度对此标题的评价: 使用字体图标的优势 字体图标除了图像清晰度之外,比位图还有哪些优势呢. 适用性:一个图标字体比一系列的图像(特别是在Retina屏中使用双倍大小的图像)要小.一旦图标字体加载 ...
- Django自定义过滤器中is_safe和need_autoescape两个参数的理解
自定义template过滤器的方法参考文档,不再赘述 is_safe 文档说明过滤的两种最终形态,其中一种是设置register.filter(is_safe=True),但是对is_safe的具体作 ...
- win10 uwp 访问解决方案文件
本文讲如何访问解决方案的资源. 我们经常会把一些图片资源放在我们的解决方案,那么从这里拿出来很简单. 我在 Assets 放了图片 1.jpg 那么我要把他拿出来可以 <Image Source ...
- 什么是UUID?
1.定义 UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundatio ...
- js 关于性能的数据存储
1.JavaScript中四种基本数据存取位置:字面量,本地变量,数组元素,对象成员.(四个都是存在于内存中) 一般来说:[字面量,局部变量]运行速度>[数组,对象成员] 2.内部属性包含了一个 ...
- APP崩溃提示:This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
崩溃输出日志 2017-08-29 14:53:47.332368+0800 HuiDaiKe[2373:1135604] This application is modifying the auto ...
- dubbo的架构
dubbo架构图如下所示: 节点角色说明: Provider: 暴露服务的服务提供方. Consumer: 调用远程服务的服务消费方. Registry: 服务注册与发现的注册中心. Monitor: ...
- HDFS 简介
hadoop分别从3个角度将主机划分为2种角色 最基本的是Master 和 从HDFS角度,将主机划分为namenode和datanode,在分布式文件系统中,目录管理很重要,管理目录相当于主人 从m ...
- MongoDB原子操作
MongoDB原子操作常用命令: 1. $set: 用来指定一个键并更新键值,若键不存在则创建并赋值. { $set : { field : value } } 2. $unset: 用来删除一个键. ...
- LeetCode 40. Combination Sum II (组合的和之二)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...