0%

PEP 20 -- Python 之禅

PEP: 20
标题: The Zen of Python
作者: tim.peters at gmail.com (Tim Peters)
译者: 刘硕
状态: 有效
类型: Informational
创建日期: 2004 年 8 月 19 日
发布历史: 2004 年 8 月 22 日

概述

资深 Python 开发者 Tim Peters 简洁地使用 20 行格言阐述了终身仁慈独裁者(BDFL)的 Python 设计指导原则,这其中的 19 行被写在了此处。

Python 之禅

原文:

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

译文:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
好看的代码比难看的代码强。
清晰直白的代码比语义模糊的代码强。
简单的代码比复合的代码强。
复合的代码比复杂的代码强。
层次简单的代码比嵌套的代码强。
稀疏的代码比稠密的代码强。
写让人看得懂的代码。
再特殊的情况也不应该特殊到能够打破规则。
即便能让代码更实用也不能让代码乱成一团。
错误不应该被毫无声息地被忽略掉。
不过可以很明确地忽略错误。
面对歧义的时候,不要试图让别人去猜。
应该有一个——最好只有一个——明显的方法完成任务。
不过如果你不是荷兰人的话,那个方法可能一开始的时候没有那么明确。
做比不做强。
不过不做通常比现在就做强。
如果实现的方式很难解释,这就不是一个好办法。
如果实现的方式很容易解释,那么它有可能是一个好办法。
命名空间是一个贼鸡儿好的办法——咱们多做点这种事吧!

复活节彩蛋

1
>>> import this

参考文献

本文最早发表在 comp.lang.python/[email protected] 下的一个名叫“The Way of Python”话题中 https://groups.google.com/d/msg/comp.lang.python/B_VxeTBClM0/L8W9KlsiriUJ

版权声明

本文档原文被放在公开的域名。

原文地址:https://github.com/python/peps/blob/master/pep-0020.txt