一、核心意义与机制
1.1 构造过程原理

1.2 与 __init__ 对比
| 特性 | __new__ | __init__ |
|---|---|---|
| 方法类型 | 静态方法 | 实例方法 |
| 返回值 | 必须返回实例对象 | 无返回值 |
| 调用时机 | 创建实例时首先调用 | 在 __new__ 之后调用 |
| 主要职责 | 控制实例创建过程 | 初始化实例属性 |
二、核心功能解析
2.1 核心能力
- 控制实例创建过程
- 决定是否生成新实例
- 修改实例创建逻辑
- 实现设计模式底层支持
2.2 方法签名
元类中的 __new__ 参数(示例 4.1)
- 样例
class meta(type):
def __new__(mcs, name, bases, attrs):
# 参数列表固定
return super().__new__(mcs, name, bases, attrs)- 参数解析表
| 参数名 | 类型 | 说明 |
|---|---|---|
| mcs | type | 元类自身(约定命名,类似 cls 代表类) |
| name | str | 要创建的类名(如 "myclass") |
| bases | tuple | 基类列表(继承的父类) |
| attrs | dict | 类属性字典(包含方法、类变量等) |
调用逻辑
- 元类用于创建类对象(不是实例对象)
- 参数由解释器在定义类时自动传入
super().__new__最终调用type.__new__生成类对象
不可变类型子类的 __new__(示例 3.2)
样例
class immutablestr(str):
def __new__(cls, value):
return super().__new__(cls, processed_value)- 参数解析表
| 参数名 | 类型 | 说明 |
|---|---|---|
| cls | type | 当前类对象(immutablestr) |
| value | any | 用户自定义参数(初始化输入值) |
调用逻辑
- 继承自不可变类型(
str/int/tuple等) - 必须通过
__new__完成实例创建 super().__new__调用父类(str)的构造方法- 参数需匹配父类
__new__的要求(如str需要传入初始化字符串)
可变类型普通类的 __new__(示例 3.1)
样例
class singleton:
def __new__(cls, *args, **kwargs):
return super().__new__(cls)- 参数解析表
| 参数名 | 类型 | 说明 |
|---|---|---|
| cls | ` | 当前类对象(singleton) |
| *args | tuple | 位置参数(与 __init__ 共享参数) |
| **kwargs | dict | 关键字参数(与 __init__ 共享参数) |
调用逻辑
- 普通类的实例创建流程
super().__new__调用object.__new__生成实例- 参数需与
__init__方法兼容
2.3 参数传递关系图示

2.4 核心记忆要点
元类 __new__ 的四个参数是固定结构
- 用于构建类对象(类的模板)
- 参数由解释器自动填充
普通类 __new__ 第一个参数必为 cls
- 后续参数需与
__init__匹配 - 不可变类型需要完全重写参数列表
super().__new__ 的参数必须与父类一致
- 元类中:
super().__new__(mcs, name, bases, attrs) - 普通类中:
super().__new__(cls[, ...])
三、典型应用场景
3.1 单例模式实现
class singleton:
_instance = none
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super().__new__(cls)
return cls._instance
a = singleton()
b = singleton()
print(a is b) # true3.2 不可变类型扩展
class immutablestr(str):
def __new__(cls, value):
# 预处理字符串
processed = value.strip().upper()
return super().__new__(cls, processed)
s = immutablestr(" hello ")
print(s) # "hello"3.3 对象池技术
class connectionpool:
_pool = []
_max_size = 5
def __new__(cls):
if len(cls._pool) < cls._max_size:
obj = super().__new__(cls)
cls._pool.append(obj)
return obj
return cls._pool.pop(0)
conn1 = connectionpool()
conn2 = connectionpool()四、高级应用技巧
4.1 元类协作
class meta(type):
def __new__(mcs, name, bases, attrs):
# 添加类属性
attrs['version'] = 1.0
return super().__new__(mcs, name, bases, attrs)
class myclass(metaclass=meta):
pass
print(myclass.version) # 1.04.2 参数预处理
class smarttuple(tuple):
def __new__(cls, iterable):
# 过滤非数字元素
filtered = (x for x in iterable if isinstance(x, (int, float)))
return super().__new__(cls, filtered)
t = smarttuple([1, 'a', 3.14, none])
print(t) # (1, 3.14)五、继承体系中的使用
5.1 继承链处理
class base:
def __new__(cls, *args, **kwargs):
print(f"creating {cls.__name__}")
return super().__new__(cls)
class child(base):
pass
c = child() # 输出 "creating child"5.2 多继承处理
class a:
def __new__(cls, *args, **kwargs):
print("a's __new__")
return super().__new__(cls)
class b:
def __new__(cls, *args, **kwargs):
print("b's __new__")
return super().__new__(cls)
class c(a, b):
def __new__(cls, *args, **kwargs):
return a.__new__(cls)
obj = c() # 输出 "a's __new__"六、注意事项与调试
6.1 常见错误
class errorcase:
def __new__(cls):
# 错误:忘记返回实例
print("creating instance") # ❌ 无返回值
def __init__(self):
print("initializing")
e = errorcase() # typeerror6.2 调试技巧
class debugclass:
def __new__(cls, *args, **kwargs):
print(f"__new__ args: {args}")
instance = super().__new__(cls)
print(f"instance id: {id(instance)}")
return instance
def __init__(self, value):
print(f"__init__ value: {value}")
d = debugclass(42)七、性能优化建议
7.1 对象缓存策略
class expensiveobject:
_cache = {}
def __new__(cls, config):
key = hash(frozenset(config.items()))
if key not in cls._cache:
instance = super().__new__(cls)
instance._init(config)
cls._cache[key] = instance
return cls._cache[key]
def __init__(self, config):
# 避免重复初始化
self.config = config最佳实践总结
- 优先使用
super().__new__保证继承链正常 - 修改不可变类型必须使用
__new__ - 单例模式要处理好线程安全问题
- 避免在
__new__中做耗时操作
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论