大家好,我是程序员晚枫。

Python 有 19 条"宪法"——

它叫"Python 之禅"(The Zen of Python),写在 PEP 20 里

这 19 条话,是 Python 设计的灵魂。

理解这 19 条,你就理解了 Python 为什么会这样设计。

今天这篇文章,一条一条讲给你听。


一、什么是 Python 之禅?

Python 之禅(The Zen of Python) 是 Python 最重要的设计哲学。

作者:Tim Peters

位置:PEP 20

完整版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

这是 Python 的"宪法"——所有 Python 代码都应该遵循。


二、19 条之禅逐条解读

第 1 条:Beautiful is better than ugly.

翻译:美优于丑。

白话:代码要写得漂亮

例子

1
2
3
4
5
6
# 丑的代码
def f(x,y):return x*x+y*y

# 美的代码
def calculate_sum_of_squares(x, y):
return x ** 2 + y ** 2

第 2 条:Explicit is better than implicit.

翻译:明确优于隐晦。

白话不要让别人猜你的代码。

例子

1
2
3
4
5
# 隐晦的代码
data = [x for x in range(10)] # x 是什么?

# 明确的代码
numbers = [num for num in range(10)] # 名字说明一切

第 3 条:Simple is better than complex.

翻译:简单优于复杂。

白话能用简单的方法就别用复杂的

例子

1
2
3
4
5
# 复杂的代码
result = list(map(lambda x: x**2, filter(lambda x: x % 2 == 0, range(10))))

# 简单的代码
result = [x**2 for x in range(10) if x % 2 == 0]

第 4 条:Complex is better than complicated.

翻译:复杂优于难懂。

白话复杂可以,但别难懂

例子

  • 一个复杂的算法,每一步都清晰可见——这就叫"复杂但易懂"
  • 一个简单的逻辑,写了一堆神奇符号——这就叫"看似简单但难懂"

第 5 条:Flat is better than nested.

翻译:扁平优于嵌套。

白话少用嵌套

例子

1
2
3
4
5
6
7
8
9
10
11
# 嵌套的代码
if a:
if b:
if c:
do_something()

# 扁平的代码
if not a: return
if not b: return
if not c: return
do_something()

第 6 条:Sparse is better than dense.

翻译:稀疏优于密集。

白话多留白,别把代码挤一起

例子

1
2
3
4
5
6
# 密集的代码
def foo(x,y,z):return x+y+z

# 稀疏的代码
def foo(x, y, z):
return x + y + z

第 7 条:Readability counts.

翻译:可读性很重要。

白话代码是写给人看的,顺便给机器运行

金句

第 8 条:Special cases aren't special enough to break the rules.

翻译:特例不足以违反规则。

白话别因为"特殊情况"就破坏代码规范

第 9 条:Although practicality beats purity.

翻译:尽管实用性胜过纯粹性。

白话但现实比理论更重要

这是 8 和 9 的组合

  • 规则要遵守
  • 特殊情况可以灵活处理
  • 比如:性能关键代码可以破例

第 10 条:Errors should never pass silently.

翻译:错误不应该被静默忽略。

白话出错了要让人知道

例子

1
2
3
4
5
6
7
8
9
10
11
12
# 静默忽略错误
try:
do_something()
except:
pass # 错误被吞了

# 显式处理错误
try:
do_something()
except Exception as e:
logger.error(f"出错了: {e}")
raise # 抛出

第 11 条:Unless explicitly silenced.

翻译:除非显式地让它沉默。

白话如果你真的想忽略某个错误,要明确写出来

1
2
3
4
5
# 显式忽略
try:
do_something()
except SpecificError:
pass # 明确写 SpecificError,不是裸 except

第 12 条:In the face of ambiguity, refuse the temptation to guess.

翻译:面对歧义,拒绝猜测的诱惑。

白话不懂就问,别猜

第 13 条:There should be one-- and preferably only one --obvious way to do it.

翻译:应该有一种——最好只有一种——明显的方法来做。

白话Python 推崇"一种明显的方法"

这和 Perl 的"多种方法做一件事"形成对比。

第 14 条:Although that way may not be obvious at first unless you're Dutch.

翻译:尽管这种方式一开始可能不明显,除非你是荷兰人。

白话:Guido 是荷兰人,他在自嘲。

幽默的一行。

第 15 条:Now is better than never.

翻译:现在做比永远不做要好。

白话别拖延,立刻开始

第 16 条:Although never is often better than right now.

翻译:但"立刻做"经常比"现在匆忙做"要好。

白话思考后再做,比冲动做更好

15 和 16 是组合

  • 别拖
  • 但别急
  • 思考 + 行动

第 17 条:If the implementation is hard to explain, it's a bad idea.

翻译:如果实现很难解释,那就是个坏主意。

白话简单到能讲清楚 = 好设计

第 18 条:If the implementation is easy to explain, it may be a good idea.

翻译:如果实现容易解释,可能是个好主意。

白话17 和 18 的组合

  • 难解释 = 坏
  • 容易解释 = 可能好(但不一定好)

第 19 条:Namespaces are one honking great idea -- let's do more of those!

翻译:命名空间是个绝妙的主意——我们应该多用!

白话多使用命名空间,避免变量冲突

这是 Python 模块化设计的核心


三、Python 之禅的 5 大主题

19 条可以归类成 5 大主题

主题 1:美(Beauty)

  • 第 1 条:美优于丑

核心:代码要写得像诗。

主题 2:明(Explicit)

  • 第 2 条:明确优于隐晦
  • 第 12 条:拒绝猜测

核心:别让人猜。

主题 3:简(Simple)

  • 第 3 条:简单优于复杂
  • 第 5 条:扁平优于嵌套
  • 第 7 条:可读性

核心:简单最美。

主题 4:严(Strict)

  • 第 8 条:规则不可破
  • 第 10 条:错误不可忽视

核心:严格但合理。

主题 5:思(Think)

  • 第 15-16 条:现在做但不匆忙
  • 第 17-18 条:易解释的才是好设计

核心:思考 + 行动。


四、Python 之禅的实际应用

应用 1:写代码时

写完代码,问自己 5 个问题

  1. 这段代码漂亮吗?
  2. 这段代码明确吗?
  3. 这段代码简单吗?
  4. 这段代码可读吗?
  5. 这段代码容易解释吗?

5 个问题答"是",就是好代码。

应用 2:Code Review

评审同事代码时引用 Python 之禅

  • "这段不太 PEP 20,复杂了"
  • "这个可以更扁平"
  • "这个名字不够明确"

比"我觉得你写得不规范"有说服力 100 倍。

应用 3:面试

面试被问"Python 设计哲学"

  • 答 19 条之一
  • 展示你的深度
  • 吊打 99% 的面试者

应用 4:技术写作

写技术文章用 Python 之禅做小标题

  • 第 3 条:简单优于复杂
  • 第 7 条:可读性

显得专业。

应用 5:职业发展

从"码农"到"工程师"

  • 写代码 → 写好代码
  • 写好代码 → 写有设计感的代码
  • Python 之禅是从"会写"到"写好"的指南

五、3 个 Python 之禅实战案例

案例 1:重构代码

重构前

1
2
3
4
5
6
7
8
def calc(x, y, op):
if op == '+': return x + y
elif op == '-': return x - y
elif op == '*': return x * y
elif op == '/':
if y != 0: return x / y
else: return None
else: return None

重构后(遵循 Python 之禅)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def calculate(x, y, operator):
"""用 operator 模块更 Pythonic。"""
import operator
operations = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
}
if operator not in operations:
raise ValueError(f"不支持的运算符: {operator}")
if operator == '/' and y == 0:
raise ValueError("除数不能为 0")
return operations[operator](x, y)

改进点

  • 函数名更明确(calc → calculate)
  • 用 operator 模块替代 if-elif
  • 错误显式抛出

案例 2:命名空间

反例(违反第 19 条):

1
2
3
4
from module1 import *
from module2 import *

# 不知道哪个函数来自哪个模块

正例(遵循第 19 条):

1
2
3
4
5
6
import module1
import module2

# 明确知道来自哪里
module1.function()
module2.function()

案例 3:错误处理

反例(违反第 10 条):

1
2
3
4
try:
do_something()
except:
pass

正例(遵循第 10、11 条):

1
2
3
4
5
try:
do_something()
except ValueError as e:
logger.error(f"出错了: {e}")
raise

六、Python 之禅的 5 个常见误解

误解 1:"简单"等于"代码少"

  • ❌ 错
  • ✅ "简单"等于易理解不是字符少

误解 2:"明确"等于"啰嗦"

  • ❌ 错
  • ✅ "明确"等于清晰不是废话多

误解 3:"可读性"是主观的

  • ❌ 错
  • ✅ "可读性"有客观标准(PEP 8)

误解 4:"扁平"等于"没有类"

  • ❌ 错
  • ✅ "扁平"等于少嵌套不是不要类

误解 5:"Python 之禅"是规则

  • ❌ 错
  • ✅ "Python 之禅"是指导原则不是死规则

七、Python 之禅 19 条速查表

编号原文一句话翻译
1Beautiful is better than ugly.美优于丑
2Explicit is better than implicit.明确优于隐晦
3Simple is better than complex.简单优于复杂
4Complex is better than complicated.复杂优于难懂
5Flat is better than nested.扁平优于嵌套
6Sparse is better than dense.稀疏优于密集
7Readability counts.可读性很重要
8Special cases aren't special enough to break the rules.特例不破规则
9Although practicality beats purity.实用胜过纯粹
10Errors should never pass silently.错误不静默
11Unless explicitly silenced.除非显式静默
12In the face of ambiguity, refuse the temptation to guess.拒绝猜测
13There should be one obvious way to do it.一种明显方法
14Although that way may not be obvious at first unless you're Dutch.除非你是荷兰人
15Now is better than never.现在做比永远不做好
16Although never is often better than right now.但不要急
17If the implementation is hard to explain, it's a bad idea.难解释就是坏
18If the implementation is easy to explain, it may be a good idea.易解释可能是好
19Namespaces are one honking great idea!命名空间是好主意

八、最后的最后

Python 之禅这事,3 句话总结

  1. 19 条哲学:Beautiful、Explicit、Simple、Readability、Namespaces
  2. 不只装饰:写代码、Code Review、面试、职业发展都用得上
  3. 一句话记忆美、明确、简单、可读、严、思

Python 之禅是 Python 的"灵魂"。

理解它,你就理解了 Python 为什么会这样设计。

你也会写出"像 Python"的代码。

从今天开始,每写一段代码,就问自己:这段符合 Python 之禅吗?

3 个月后,你的代码会有质的飞跃。


相关阅读


科技不高冷,AI很好用。
我是晚枫,关注我,带你一起玩AI!

🎓 AI 编程实战课程

程序员晚枫专注AI编程培训,通过 《50讲 · AI编程训练营》,让小白也能用AI做出实际项目。帮你从零上手!