Python编程:从入门到实践(第3版)

> 📖 **一起读书吧!** 加入《Python编程:从入门到实践》共读营 👉 [点击参加](https://mp.weixin.qq.com/s/ehe2vMrfAFscRLqbM9TF-g)

本讲内容

  • Python之禅的20条准则
  • 每一条的真实含义和实战应用
  • Pythonic代码的思维方式

学习目标

理解Python的设计哲学,写出更优雅的代码 🧠


1. Python之禅的来源

输入以下命令,你会看到Tim Peters写的Python设计哲学:

1
import this

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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!

2. 逐条解读

Beautiful is better than ugly.

美优于丑。代码是给人看的,要讲究排版和结构。

1
2
3
4
5
6
7
# ❌ 丑
result=[]
for x in range(20):
if x%2==0:result.append(x**2)

# ✅ 美
result = [x**2 for x in range(20) if x % 2 == 0]

Explicit is better than implicit.

显式优于隐式。不要让读者猜测代码在做什么。

1
2
3
4
5
6
7
8
# ❌ 隐式:依赖魔法全局变量
total = 100
def add_tax():
return total * 1.1

# ✅ 显式:参数清晰
def add_tax(amount, tax_rate=0.1):
return amount * (1 + tax_rate)

Simple is better than complex.

简单优于复杂。能用一行代码解决的,不要写十行。

Complex is better than complicated.

复杂优于混乱。如果问题本身就复杂,不要硬简化,保持结构清晰。

Flat is better than nested.

扁平优于嵌套。少用嵌套,多用函数拆分。

1
2
3
4
5
6
7
8
9
10
# ❌ 过度嵌套
if user:
if user.is_active:
if user.is_verified:
grant_access()

# ✅ 扁平化
if not user or not user.is_active or not user.is_verified:
return
grant_access()

Sparse is better than dense.

稀疏优于紧凑。代码要有呼吸感,不要挤在一起。

Readability counts.

可读性很重要。代码阅读次数远多于编写次数。

1
2
3
4
5
6
7
8
# ❌ 不可读
d={'a':1,'b':2}
for k,v in d.items():print(k,v)

# ✅ 可读
populations = {'beijing': 2100, 'shanghai': 2400, 'shenzhen': 1700}
for city, population in populations.items():
print(f"{city}: {population}万")

Special cases aren't special enough to break the rules.

特殊不能打破规则。不要为了"特例"破坏代码规范。

Although practicality beats purity.

实用优于纯粹。但如果必须二选一,先让代码work。

Errors should never pass silently.

错误不该被悄悄忽略。捕获异常后要处理,不能 except: pass

1
2
3
4
5
6
7
8
9
10
11
# ❌ 静默失败
try:
data = json.loads(user_input)
except:
pass

# ✅ 处理错误
try:
data = json.loads(user_input)
except json.JSONDecodeError as e:
print(f"JSON格式错误: {e}")

Unless explicitly silenced.

除非明确忽略。如果真的不需要处理,用 pass 或日志记录。

In the face of ambiguity, refuse the temptation to guess.

遇到歧义时,不要猜测。写清楚,不要靠"应该没问题"。

There should be one-- and preferably only one --obvious way to do it.

应该有一个(且最好只有一个)显而易见的方式
Python的哲学是"有且仅有最佳实践",不要搞多种写法。

Although that way may not be obvious at first unless you're Dutch.

指Python之父Guido van Rossum(荷兰人)——创始人的审美决定了这门语言的方向。

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!

命名空间是个绝妙的想法。用模块分隔命名空间避免冲突。

1
2
3
# 不同命名空间,不会冲突
import collections
import my_module.collections # 两者共存

3. 在实践中运用

下次写代码前,先想想:

  • 我的代码"美"吗?
  • 我的代码"显式"吗?
  • 我的函数是"一个功能"吗?
  • 我的变量名"可读"吗?

这4个问题能帮你规避80%的代码坏味道。


📚 官方文档参考


🎓 AI 编程实战课程

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