在 Python 中,类变量和实例变量的命名没有强制的语法约束(不像关键字),但遵循PEP 8 规范和行业通用惯例能极大提升代码可读性,核心原则是「清晰区分变量归属、符合 Python 命名风格」。下面分两类变量讲透命名规范,包括核心规则、避坑点和最佳实践。
一、通用基础命名规范(适用于所有变量)
先明确 Python 变量命名的通用规则(类变量 / 实例变量均需遵守):
- 命名风格:全部使用小写字母,单词之间用下划线
_分隔(蛇形命名法,snake_case),这是 PEP 8 对变量的核心要求; - 禁止使用:
- 关键字(如
class、def、self、if等); - 单下划线开头 + 大写字母(如
_ABC)(通常是模块内私有常量); - 双下划线开头且结尾(如
__init__)(Python 魔法方法 / 属性,保留字);
- 关键字(如
- 长度与语义:名称要体现变量的用途(见名知意),避免单字母(除了通用缩写,如
i表示循环索引、x/y表示坐标); - 禁止特殊字符:只能包含字母、数字、下划线,且不能以数字开头。
二、类变量的命名规范
类变量是「类的共享属性」,命名需突出「全局 / 共享 / 常量」的特性,核心规则如下:
| 类变量类型 | 命名规则 | 示例 |
|---|---|---|
| 普通共享类变量 | 蛇形命名法,体现 “共享 / 统计 / 全局” 语义 | total_count、default_brand、class_name |
| 常量类变量(推荐) | 全大写 + 下划线分隔(PEP 8 对常量的规范),表示 “不可修改的类级常量” | MAX_RETRY、DEFAULT_TIMEOUT、SPECIES |
| 私有类变量(可选) | 单下划线开头 + 蛇形命名(表示 “模块内私有,不建议外部访问”,仅约定,非强制) | _internal_cache、_temp_data |
示例:类变量命名
python
运行
class Student: # 常量类变量(全大写) MAX_SCORE = 100 DEFAULT_CLASS = "高一(1)班" # 普通共享类变量(蛇形命名) total_students = 0 pass_rate = 0.85 # 私有类变量(单下划线开头) _grade_mapping = {1: "一年级", 2: "二年级"} def __init__(self, name, score): self.name = name self.score = score # 修改类变量时遵循命名规范 Student.total_students += 1类变量命名避坑点:
- 不要用全小写命名常量类变量(如
maxscore),无法区分 “可变共享变量” 和 “不可变常量”; - 不要用双下划线开头(如
__total):双下划线开头会触发 Python 的「名称改写(name mangling)」,变成_类名__变量名,仅用于 “类内私有” 的极端场景,非必要不要用; - 避免与实例变量同名:如类变量
name和实例变量self.name同名,会导致实例访问时优先读实例变量,逻辑混乱(示例:class Student: name = "默认名"; def __init__(self, name): self.name = name)。
三、实例变量的命名规范
实例变量是「单个实例的独有属性」,命名需突出「实例专属 / 个体特征」,核心规则如下:
| 实例变量类型 | 命名规则 | 示例 |
|---|---|---|
| 普通实例变量 | 蛇形命名法,体现 “实例独有 / 个体特征” 语义 | name、age、phone_number、order_id |
| 私有实例变量(推荐) | 单下划线开头 + 蛇形命名(约定 “类内私有,外部不建议访问”,非语法强制) | _password、_internal_state |
| 强私有实例变量(慎用) | 双下划线开头 + 蛇形命名(触发名称改写,防止子类覆盖,仅特殊场景用) | __secret_key |
示例:实例变量命名
python
运行
class Order: # 类变量(常量) MAX_ITEMS = 20 def __init__(self, order_id, user_name, total_amount): # 普通实例变量 self.order_id = order_id self.user_name = user_name self.total_amount = total_amount self.items = [] # 私有实例变量(单下划线) self._payment_status = "unpaid" # 强私有实例变量(双下划线,慎用) self.__transaction_id = None def pay(self, trans_id): # 类内可访问私有实例变量 self._payment_status = "paid" self.__transaction_id = trans_id实例变量命名避坑点:
- 不要滥用双下划线:双下划线会增加代码复杂度(如子类无法直接访问、调试困难),仅当需要 “防止子类意外覆盖” 时使用;
- 避免无意义命名:如
self.x、self.data1,应改为self.coordinate_x、self.order_data; - 不要与内置属性 / 方法同名:如
self.list、self.dict(应改为self.item_list、self.data_dict),避免覆盖内置类型。
四、命名规范总结(核心对照表)
| 维度 | 类变量 | 实例变量 |
|---|---|---|
| 核心风格 | 蛇形命名(普通)/ 全大写(常量) | 蛇形命名(普通)/ 单下划线(私有) |
| 语义重点 | 共享、全局、常量 | 个体、独有、状态 |
| 私有标识 | 单下划线(约定) | 单下划线(推荐)/ 双下划线(慎用) |
| 禁止操作 | 与实例变量同名、双下划线开头 | 滥用双下划线、与内置名同名 |
五、最佳实践建议
- 常量类变量必用全大写:如
MAX_RETRY = 3,一眼能识别 “不可修改的类级常量”; - 实例变量优先单下划线私有:对外暴露的属性用普通命名,内部状态用单下划线(如
self._status),符合 “最小暴露原则”; - 避免同名冲突:类变量和实例变量的名称尽量不重复,若必须重复(如特殊业务),需在注释中明确说明;
- 团队统一规范:如果团队有自定义命名规则(如前缀
cls_标识类变量),优先遵循团队规范(如cls_total_count),可读性优先于 “纯 PEP 8”。
遵循以上规范,既能符合 Python 官方标准,又能让其他开发者一眼区分 “类变量” 和 “实例变量”,大幅降低代码维护成本。
https://avg.163.com/topic/detail/8012750
https://avg.163.com/topic/detail/8012758
https://avg.163.com/topic/detail/8013192
https://avg.163.com/topic/detail/8013926
https://avg.163.com/topic/detail/8013926
https://avg.163.com/topic/detail/8013547
https://avg.163.com/topic/detail/8012753
https://avg.163.com/topic/detail/8013198
https://avg.163.com/topic/detail/8013548
https://avg.163.com/topic/detail/8012761
https://avg.163.com/topic/detail/8013194
https://avg.163.com/topic/detail/8013558
https://avg.163.com/topic/detail/8013921
https://avg.163.com/topic/detail/8014339
https://avg.163.com/topic/detail/8012747
https://avg.163.com/topic/detail/8012762
https://avg.163.com/topic/detail/8013560
https://avg.163.com/topic/detail/8013932
https://avg.163.com/topic/detail/8014336
https://avg.163.com/topic/detail/8013182
https://avg.163.com/topic/detail/8013551
https://avg.163.com/topic/detail/8013924
https://avg.163.com/topic/detail/8012756
https://avg.163.com/topic/detail/8013187
https://avg.163.com/topic/detail/8013553
https://avg.163.com/topic/detail/8012759
https://avg.163.com/topic/detail/8013559
https://avg.163.com/topic/detail/8013930
https://avg.163.com/topic/detail/8014325
https://avg.163.com/topic/detail/8012329
https://avg.163.com/topic/detail/8013928
https://avg.163.com/topic/detail/8012744
https://avg.163.com/topic/detail/8013184
https://avg.163.com/topic/detail/8014330
https://avg.163.com/topic/detail/8013919
https://avg.163.com/topic/detail/8014322
https://avg.163.com/topic/detail/8013913
https://avg.163.com/topic/detail/8012590
https://avg.163.com/topic/detail/8014324
https://avg.163.com/topic/detail/8014494
https://avg.163.com/topic/detail/8014319
https://avg.163.com/topic/detail/8012324
https://avg.163.com/topic/detail/8013916
https://avg.163.com/topic/detail/8014496
https://avg.163.com/topic/detail/8013188
https://avg.163.com/topic/detail/8014321
https://avg.163.com/topic/detail/8014541
https://avg.163.com/topic/detail/8013555
https://avg.163.com/topic/detail/8014499
https://avg.163.com/topic/detail/8013914
https://avg.163.com/topic/detail/8012588
https://avg.163.com/topic/detail/8012325
https://avg.163.com/topic/detail/8014320
https://avg.163.com/topic/detail/8014493
https://avg.163.com/topic/detail/8012595
https://avg.163.com/topic/detail/8014545
https://avg.163.com/topic/detail/8014491
https://avg.163.com/topic/detail/8012328
https://avg.163.com/topic/detail/8014543
https://avg.163.com/topic/detail/8014614
https://avg.163.com/topic/detail/8014544
https://avg.163.com/topic/detail/8012593
https://avg.163.com/topic/detail/8012326
https://avg.163.com/topic/detail/8014612
https://avg.163.com/topic/detail/8014613
https://avg.163.com/topic/detail/8014498
https://avg.163.com/topic/detail/8012591
https://avg.163.com/topic/detail/8014540
https://avg.163.com/topic/detail/8014495
https://avg.163.com/topic/detail/8014610
https://avg.163.com/topic/detail/8012323
https://avg.163.com/topic/detail/8014537
https://avg.163.com/topic/detail/8012322
https://avg.163.com/topic/detail/8014608
https://avg.163.com/topic/detail/8012587
https://avg.163.com/topic/detail/8012589
https://avg.163.com/topic/detail/8014490
https://avg.163.com/topic/detail/8014492
https://avg.163.com/topic/detail/8014611
https://avg.163.com/topic/detail/8012319
https://avg.163.com/topic/detail/8012586
https://avg.163.com/topic/detail/8014489
https://avg.163.com/topic/detail/8014538
https://avg.163.com/topic/detail/8012321
https://avg.163.com/topic/detail/8014488
https://avg.163.com/topic/detail/8014536