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
()方法是非常合理的。那就是下

一节将介绍的元类。

相关推荐
AI探索者6 小时前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者6 小时前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh8 小时前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅8 小时前
Python函数入门详解(定义+调用+参数)
python
曲幽9 小时前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时12 小时前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿15 小时前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户8356290780511 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng81 天前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi1 天前
Chapter 2 - Python中的变量和简单的数据类型
python