





















Python 社区里,对名字中的下划线有一套约定术语:
| 形式 | 名称 | 例子 |
|---|---|---|
name |
普通名称 | value |
_name |
single leading underscore | _cache |
name_ |
single trailing underscore | class_ |
__name |
double leading underscore | __value |
__name__ |
dunder name | __init__ |
_name_ |
sunder name | _missing_ |
其中:
__dunder___sunder_主要出现在:
Enumdataclasstypingdunder =
double underscore
即:
__name__
这种:
前后各两个下划线
又叫:
__init__
__new__
__call__
__iter__
__len__
__dict__
__class__
这是:
Python 语言级协议接口
例如:
len(x)
实际上调用:
x.__len__()
a + b
本质:
a.__add__(b)
for x in obj:
本质:
iter(obj)
→ obj.__iter__()
Python 解释器保留协议名
用于:
等等。
你最好不要乱发明:
__myfunc__
因为未来 Python 可能占用。
PEP 明确建议:
用户代码不要随意定义新的 dunder 名字。
例如:
obj[0]
其实:
obj.__getitem__(0)
所以:
sunder =
single underscore
即:
_name_
特点:
不像 dunder 属于解释器。
sunder 更多是:
标准库/框架保留命名空间
最典型:
来自 Python 的 enum 模块。
例如:
_missing_
_generate_next_value_
_ignore_
因为:
它不能污染 Python 语言层。
因此:
_missing_
表示:
“Enum框架保留接口”
而不是:
“Python解释器协议”
from enum import Enum
class Color(Enum):
RED = 1
@classmethod
def _missing_(cls, value):
return cls.RED
现在:
Color(999)
不会报错,而会:
Color.RED
因为:
EnumMeta
内部会调用:
_missing_
| 对比 | dunder | sunder |
|---|---|---|
| 形式 | __x__ |
_x_ |
| 含义 | 解释器协议 | 库协议 |
| 谁调用 | Python VM | 框架/库 |
| 是否语言级 | ✅ | 🚫 |
| 是否官方保留 | 强保留 | 弱保留 |
| 用户能否发明 | 不建议 | 更不建议冲突 |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。