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.

这里有几条非常重要

  1. new 接受的参数除了 cls 还可以有很多。 剩余的参数其实是会传递给__init__
  2. 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()

最新文章

  1. PHP array_multisort—对多个数组或多维数组进行排序
  2. JSP导入EXCEL样式
  3. Authentication和Authorization的区别
  4. JAVA:三种集合LIST、SET、MAP
  5. ie6兼容性,还需要测试么?迷茫。。。
  6. Openjudge-计算概论(A)-回文串判断
  7. C#图片的读取和转码
  8. 咱家自己的vim配置
  9. python3-基础2
  10. Jenkins Pipline语法
  11. 使用Jmeter创建ActiveMQ JMS POINT TO POINT请求,环境搭建、请求创建、插件安装、监听服务器资源等
  12. MVC框架-.net-摘
  13. 20155222卢梓杰 实验八 Web基础
  14. java基础篇---网络编程(TCP程序设计)
  15. js中级总结
  16. 一些简单的SQL Server服务器监控
  17. Java中正则匹配性能测试
  18. PTA基础编程题目集6-4求自定类型元素的平均 (函数题)
  19. three.js入门系列之材质
  20. 认识HTML中文本、图片、链接标签和路径

热门文章

  1. 请大神看看10.10Beta1的AppleRTC怎么破?原版会导致BIOS重置!
  2. Fortran学习记录3(选择语句)
  3. bzoj3545 [ONTAK2010]Peaks、bzoj3551 [ONTAK2010]Peaks加强版
  4. 初涉「带权并查集」&&bzoj3376: [Usaco2004 Open]Cube Stacking 方块游戏
  5. 【贪心 思维题】[USACO13MAR]扑克牌型Poker Hands
  6. (12)zabbix agent 类型所有key
  7. History Api以及hash操作
  8. PyCharm 社区版创建Django项目的一个方法
  9. C语言文件操作 FILE结构体
  10. set的应用:UVa10815-Andy's First Dictionary