__new__ 和 __init__
new 在新式类中负责真正的实例化对象,而__init__只是负责初始化 __new__创建的对象。一般来说 new 创建一个内存对象,也就是实例化的对象的实体,交给__init__进行进一步加工。官方文档如下:
https://docs.python.org/2/reference/datamodel.html#object.new
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.
这里有几条非常重要
- new 接受的参数除了 cls 还可以有很多。 剩余的参数其实是会传递给__init__
- new 返回的对象是 cls 的实例,才会调用__init__ 否则就不调用了。
知道了以上信息,我们可以写一个proxy类, 该类的作用是代理其它任何类。 比如:
proxy = Proxy(apple)
我们这里接受了一个对象apple作为参数,那么proxy 就应该有apple的所有属性。 实现方式如下:
首先定义一个类,new 方法会接受 cls, target, *args, **kwargs为参数。这里cls 就是Proxy自己,但我们在真正实例化对象的时候用target的类来实例化。
class Proxy(object):
def __new__(cls, target, *args, **kwargs):
return target.__class__(*args, **kwargs)
接下来定义两个类,有自己的方法
class A(object):
def run(self):
return 'run'
class B:
def fly(self):
return 'fly'
可以看到,我们可以用proxy 来代理 a,b 因为proxy在实例化的时候实际上借助了a/b
a = A()
b = B()
pa = Proxy(a)
pb = Proxy(b)
print "pa.run is ", pa.run()
print "pb.fly is ", pb.fly()
__new__ 和 __init__的更多相关文章
- Python中的__new__和__init__
Python中的__new__和__init__ 写了这么多的class,现在才知道还有个__new__方法, 那么它和__init__有什么区别呢? class TestCls(): "& ...
- 飘逸的python - __new__、__init__、__call__傻傻分不清
__new__: 对象的创建,是一个静态方法.第一个參数是cls.(想想也是,不可能是self,对象还没创建,哪来的self) __init__ : 对象的初始化, 是一个实例方法,第一个參数是sel ...
- Python中__new__和__init__区别
__new__:创建对象时调用,会返回当前对象的一个实例 __init__:创建完对象后调用,对当前对象的一些实例初始化,无返回值 1.在类中,如果__new__和__init__同时存在,会优先调用 ...
- 1.(python)__new__与__init__
1.来比较一下__new__与__init__: (1)__new__在初始化实例前调用,__init__在初始化实例之后调用,用来初始化实例的一些属性或者做一些初始操作 # -*- coding: ...
- __new__、__init__、__call__三个特殊方法
用双下划线包围的特殊方法在Python中又被成为魔术方法,类似于C++等语言中的构造函数,这里我们就来详解Python中的__new__.__init__.__call__三个特殊方法: 1.__ne ...
- python中的__new__与__init__,新式类和经典类(2.x)
在python2.x中,从object继承得来的类称为新式类(如class A(object))不从object继承得来的类称为经典类(如class A()) 新式类跟经典类的差别主要是以下几点: 1 ...
- python 中的__new__与__init__
在Python中的class中有两个方法__new__与__init__,有什么区别呢? class TestCls(): """docstring for TestCl ...
- Python系列之 __new__ 与 __init__
很喜欢Python这门语言.在看过语法后学习了Django 这个 Web 开发框架.算是对 Python 有些熟悉了.不过对里面很多东西还是不知道,因为用的少.今天学习了两个魔术方法:__new__ ...
- python中的__new__、__init__和__del__
__new__.__init__.__del__三个方法用于实例的创建和销毁,在使用python的类中,我们最常用的是__init__方法,通常称为构造方法,__new__方法几乎不会使用,这篇文章是 ...
- Python 中的__new__和__init__的区别
[同] 二者均是Python面向对象语言中的函数,__new__比较少用,__init__则用的比较多. [异] __new__是在实例创建之前被调用的,因为它的任务就是创建实例然后返回该实例对象,是 ...
随机推荐
- windows下pycharm使用Anaconda安装包环境
转自: https://www.cnblogs.com/heitaoq/p/8632315.html
- centos7 中安装 mysql5.6 的过程
前提是Centos的环境是好的,并且相关的软件包已经安装好. 1.创建用户,并修改创建的数据目录的属主 [root@bogon ~]# useradd -M mysql -s /sbin/nologi ...
- PHP方法之 substr
简单描述: substr 主要用于字符串的截取,但是不适用于中文字符串,易出现乱码,中文字符串可使用mbstring. 方法申明: substr(string,start,length) string ...
- python--MySQL 库,表的详细操作
一 库操作 数据库命名规则 可以由数字,字母,下划线,@, #, $ 区分大小写 唯一性 不能使用关键字如 create select 不能单独使用数字 最长128位 # 这些是对上次的补充. 二 ...
- python爬虫入门四:BeautifulSoup库(转)
正则表达式可以从html代码中提取我们想要的数据信息,它比较繁琐复杂,编写的时候效率不高,但我们又最好是能够学会使用正则表达式. 我在网络上发现了一篇关于写得很好的教程,如果需要使用正则表达式的话,参 ...
- cf 1016D
D. Vasya And The Matrix time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Python 对Mysql的操作
Mysql链接不同的数据库 如果python的模板是按照mysql来写的,后面数据库更换为了Oracle,难道需要重现再来写,当然不是,python提供了API接口,只要编写是面对api,后面的链接会 ...
- 练习题,新建数据库anyun
一.新建数据库,数据库名为anyun mysql> create database anyun; Query OK, 1 row affected (0.00 sec) 二.查看数据库 mysq ...
- 浅谈中途相遇攻击--meet-in-the-middle attack
貌似挖的坑也够多了....好多都没填,这篇最后会不会TJ还得看心情TUT 看过大白书的人应该都会发现一种神奇的算法:中途相遇法.(在第58页)这种算法将以空间换时间的思路运用到了极致,但事实上它在密码 ...
- BZOJ 3750: [POI2015]Pieczęć 【模拟】
Description 一张n*m的方格纸,有些格子需要印成黑色,剩下的格子需要保留白色. 你有一个a*b的印章,有些格子是凸起(会沾上墨水)的.你需要判断能否用这个印章印出纸上的图案.印的过程中需要 ...