Python中的__new__和__init__

写了这么多的class,现在才知道还有个__new__方法, 那么它和__init__有什么区别呢?

class TestCls():
"""docstring for TestCls""" def __init__(self, name):
print('init')
print(self)
print(type(self))
self.name = name def __new__(cls, name):
print('new')
print(cls)
print(type(cls))
return super().__new__(cls) c = TestCls("CooMark") # new...
# <class '__main__.TestCls'>
# <class 'type'> # init...
# <__main__.TestCls object at 0x02201130>
# <class '__main__.TestCls'>

异同点


1. 参数
* \_\_new\_\_的第一个占位参数是class对象
* \_\_init\_\_的第一个占位参数是class的实例对象
* 其他的参数应一致
2. 作用
* \_\_new\_\_ 用来创建实例,在返回的实例上执行\_\_init\_\_,如果不返回实例那么\_\_init\_\_将不会执行
* \_\_init\_\_ 用来初始化实例,设置属性什么的

利用__new__我们能做哪些牛逼的事情?


>Python文档
>>object.__new__(cls[, ...])
>>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 of cls).
>>
>>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.
>>
>>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.
>>

依照Python官方文档的说法,__new__方法主要是当你继承一些不可变的class时(比如int, str, tuple), 提供给你一个自定义这些类的实例化过程的途径。还有就是实现自定义的metaclass

假如我们需要一个永远都是正数的整数类型,通过集成int,我们可能会写出这样的代码

class PositiveInteger(int):

    def __init__(self, value):
super().__init__(self, abs(value)) i = PositiveInteger(-3)
print(i)
# # TypeError: object.__init__() takes no parameters class PositiveInteger(int): def __new__(cls, value):
return super(PositiveInteger, cls).__new__(cls, abs(value))
i = PositiveInteger(-3)
print(i)
# 3
  • 用__new__来实现单例
class Singleton(object):
def __new__(cls):
# 关键在于这,每一次实例化的时候,我们都只会返回这同一个instance对象
if not hasattr(cls, 'instance'):
cls.instance = super(Singleton, cls).__new__(cls)
return cls.instance
obj1 = Singleton()
obj2 = Singleton()
obj1.attr1 = 'value1'
print( obj1.attr1, obj2.attr1)
print( obj1 is obj2)
 

Python中的__new__和__init__的更多相关文章

  1. 一个案例深入Python中的__new__和__init__

    准备 在Python中,一切皆对象. 既然一切皆对象,那么类也是对象,我们暂且称之为 类对象.来个简单例子(本篇文章的所有案例都是运行在Python3.4中): class foo(): pass p ...

  2. python中的__new__与__init__,新式类和经典类(2.x)

    在python2.x中,从object继承得来的类称为新式类(如class A(object))不从object继承得来的类称为经典类(如class A()) 新式类跟经典类的差别主要是以下几点: 1 ...

  3. python 中的__new__与__init__

    在Python中的class中有两个方法__new__与__init__,有什么区别呢? class TestCls(): """docstring for TestCl ...

  4. python中的__new__、__init__和__del__

    __new__.__init__.__del__三个方法用于实例的创建和销毁,在使用python的类中,我们最常用的是__init__方法,通常称为构造方法,__new__方法几乎不会使用,这篇文章是 ...

  5. Python 中的__new__和__init__的区别

    [同] 二者均是Python面向对象语言中的函数,__new__比较少用,__init__则用的比较多. [异] __new__是在实例创建之前被调用的,因为它的任务就是创建实例然后返回该实例对象,是 ...

  6. 详解Python中的__new__、__init__、__call__三个特殊方法(zz)

    __new__: 对象的创建,是一个静态方法,第一个参数是cls.(想想也是,不可能是self,对象还没创建,哪来的self)__init__ : 对象的初始化, 是一个实例方法,第一个参数是self ...

  7. Python中的__new__()方法与实例化

    @Python中的__new__()方法与实例化   __new__()是在新式类中新出现的方法,它作用在构造方法建造实例之前,可以这么理解,在Python 中 存在于类里面的构造方法__init__ ...

  8. Python中:self和__init__的含义 + 为何要有self和__init__

    Python中:self和__init__的含义 + 为何要有self和__init__ 背景 回复: 我写的一些Python教程,需要的可以看看 中SongShouJiong的提问: Python中 ...

  9. 转载--------Python中:self和__init__的含义 + 为何要有self和__init__

    背景 回复:我写的一些Python教程,需要的可以看看,中SongShouJiong的提问: Python中的self,__init__的含义是啥?为何要有self,__init这些东西? 解释之前, ...

随机推荐

  1. IIS 部署 node.js ---- 基础安装部署

    一些可能有用的相关文章: https://blogs.msdn.microsoft.com/scott_hanselman/2011/11/28/window-iisnode-js/ http://b ...

  2. [UCSD白板题] Greatest Common Divisor

    Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...

  3. Windows下Spark单机环境配置

    1. 环境配置 a)  java环境配置: JDK版本为1.7,64位: 环境变量配置如下: JAVA_HOME为JDK安装路径,例如D:\software\workSoftware\JAVA 在pa ...

  4. 在Web应用中接入微信支付的流程之极简清晰版

    在Web应用中接入微信支付的流程之极简清晰版 背景: 在Web应用中接入微信支付,我以为只是调用几个API稍作调试即可. 没想到微信的API和官方文档里隐坑无数,致我抱着怀疑人生的心情悲愤踩遍了丫们布 ...

  5. 敏捷开发方法-Scrum

    为了不落后他人,于是我也开始学习Scrum,今天主要是对我最近阅读的相关资料,根据自己的理解,用自己的话来讲述Scrum中的各个环节,主要目的有两个,一个是进行知识的总结,另外一个是觉得网上很多学习资 ...

  6. 8.2 辅助 xUtils 3.0

    主要有四大模块: DbUtils模块: android中的orm(对象关系映射)框架,一行代码就可以进行增删改查: 支持事务,默认关闭: 可通过注解自定义表名,列名,外键,唯一性约束,NOT NULL ...

  7. Hyper-V初涉_Hyper-V虚拟机文件交换

    使用虚拟机时,文件交互就显得十分重要.如果能实现物理机与虚拟机之间的文件交互,将会节省大量的时间.比较可惜的是,Hyper-V虚拟机并不支持USB存储设备,所以在文件交换上略显麻烦. 与Hyper-V ...

  8. Android中,图片分辨率适配总结规则drawable drawable-mdpi drawable-hdpi drawable-nodpi drawable-ldpi

    一直关于android多分辨率适配有些疑惑,从网上找到一些资料并通过测试验证,参考链接:http://blog.csdn.net/lamp_zy/article/details/7686477 现记录 ...

  9. U盘的不识别问题

    1.案例: 我重新装过电脑以后出现过一个问题是: 我的U盘只能在我电脑上用,在其他电脑上不能用 其他人的U盘不能在我的电脑上用. 2.根本问题: 是驱动问题.接口上没符合的驱动,未能指定路径.电脑上可 ...

  10. 本人讲课时录制的Android应用开发技术教学视频

    网盘地址:http://yun.baidu.com/pcloud/album/info?query_uk=1963923831&album_id=3523786484935252365 本人讲 ...