Python之禅
Python之禅 最早由 Tim Peters[1][2]在Python邮件列表中发表,它包含了影响Python编程语言[3]设计的19条软件编写原则。在最初及后来的一些版本中,一共包含20条,其中第20条是「这一条留空(...)请 Guido 来填写」。这留空的一条从未公布也可能并不存在。这些文本属于 公共领域[4]。
Python之禅作为一个信息条款被录入Python增强建议(PEP)的第20条,在Python语言的官方网站也能找到。它还作为复活节彩蛋被包含在Python解释器中。如果输入 import this
就会在Python的编程环境IDLE中显示[3]。
Python之禅的内容
原文
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!
中文翻译
优美优于丑陋,
明瞭优于隐晦;
简单优于复杂,
复杂优于凌乱,
扁平优于嵌套,
稀疏优于稠密,
可读性很重要!
即使实用比纯粹更优,
特例亦不可违背原则。
错误绝不能悄悄忽略,
除非它明确需要如此。
面对不确定性,
拒绝妄加猜测。
任何问题应有一种,
且最好只有一种,
显而易见的解决方法。
尽管这方法一开始并非如此直观,
除非你是荷兰人。
做优于不做,
然而不假思索还不如不做。
很难解释的,必然是坏方法。
很好解释的,可能是好方法。
命名空间是个绝妙的主意,
我们应好好利用它。
参见
- Perl的不止一种方法去做一件事
参考文献
- ^ "The Python Way". Mail.python.org. [2018-12-27]. (原始内容存档于2019-02-12).
- ^ We Fear Change. wefearchange.org. [2019-02-26]. (原始内容存档于2019-03-27).
- ^ 3.0 3.1 Code Style. The Hitchhiker’s Guide to Python. 2015-11-18 [2019-02-26]. (原始内容存档于2018-12-26).
- ^ Tim Peters. PEP20 -- The Zen of Python. 2015-11-18 [2019-02-26]. (原始内容存档于2018-12-26).
- 1.0.1.1 “Code Style(页面存档备份,存于互联网档案馆)".
- Python之路(页面存档备份,存于互联网档案馆)
- 我们惧怕改变(页面存档备份,存于互联网档案馆)
- PEP20 - Python之禅(页面存档备份,存于互联网档案馆)