Python 高手编程系列一十五:使用 __new __()方法覆写实例创建过程

特殊方法__new__()是一种负责创建类实例的静态方法。它很特殊,所以无需使用

staticmethod 装饰器将其声明为静态方法。new (cls, ,...)方法的调用优先

于__init__()初始化方法。通常来说,覆写__new__()的实现将会使用合适的参数调用其

超类的 super().new (),并在返回之前修改实例:

class InstanceCountingClass:

instances_created = 0

def new (cls, *args, **kwargs):

print('new () called with:', cls, args, kwargs)

instance = super().new (cls)

instance.number = cls.instances_created

cls.instances_created += 1

return instance

def init (self, attribute):

print('init () called with:', self, attribute)

self.attribute = attribute

下面是在交互式会话中的日志示例,展示了 InstanceCountingClass 实现的工作

方式:

instance1 = InstanceCountingClass('abc')

__new __() called with: <class ' __main __.InstanceCountingClass'> ('abc',)

{}

__init __() called with: < __main __.InstanceCountingClass object at

0x101259e10> abc

instance2 = InstanceCountingClass('xyz')

__new __() called with: <class ' __main __.InstanceCountingClass'> ('xyz',)

{}

__init __() called with: < __main .InstanceCountingClass object at
0x101259dd8> xyz
instance1.number, instance1.instances
created
(0, 2)
instance2.number, instance2.instances
created
(1, 2)
通常来说,new ()方法应该返回该类的一个实例,但也可能返回其他类的实例。
如果发生了这种情况(即返回了其他类的实例),那么将会跳过对__init
()方法的调用。

如果需要修改不可变类实例(例如 Python 的某些内置类型)的创建行为,那么这一点是很

有用的,如下所示:

class NonZero(int):

def new (cls, value):

return super().new (cls, value) if value != 0 else None

def init(self, skipped_value):

在这个例子中可以跳过__init__的实现

但放在这里是为了展示它如何不被调用

print("init () called")

super().init ()

我们在交互式会话中查看运行结果如下:

type(NonZero(-12))

__init () called
<class ' main .NonZero'>
type(NonZero(0))
<class 'NoneType'>
NonZero(-3.123)
init () called
-3
那么什么情况下使用__new
()呢?答案很简单:只有在__init
()不够用的时候。
前面已经提到了这样的一个例子,就是对 Python 不可变的内置类型(如 int、str、float、frozenset 等)进行子类化。这是因为一旦创建了这样不可变的对象实例,就无法在
init ()方法中对其进行修改。
有些程序员可能会认为,new ()对执行重要的对象初始化可能很有用,如果用户
忘记使用 super(),可能会漏掉这一初始化。init ()调用是覆写的初始化方法。虽
然这听上去很合理,但却有一个主要的缺点。如果使用这样的方法,那么即使初始化过程
已经是预期的行为,程序员明确跳过初始化步骤也会变得更加困难。它还破坏一条潜规则,
即在__init
()中执行所有的初始化工作。
由于__new
()不限于返回同一个类的实例,所以很容易被滥用。不负责任地使用这
种方法,可能会对代码有害,因此始终应该谨慎使用,并且提供大量文档来支持。一般来
说,对于特定问题,最好搜索其他可用的解决方法,而不要影响对象创建过程,使其违背
基础程序员的预期。即使是上文提到的覆写不可变类型的初始化的例子,也可以用可预测
性更高且更加完善的设计模式来替代,例如第 14 章介绍的工厂方法。
在 Python 编程中,至少在一个方面大量使用__new
()方法是非常合理的。那就是下

一节将介绍的元类。

相关推荐
默_笙1 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
小羊没烦恼!1 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
qq_426003961 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫1 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技1 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读1 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时1 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
C语言小火车1 天前
C/C++ 为什么需要编译器?
开发语言·c++
奇思妙想聪明勤奋的小羊1 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
wuyk5551 天前
《WiFi 嵌入式物联网开发全套实战》| 第 16 章 ESP32 AP+STA 双模共存原理与工程坑点
网络·stm32·物联网