1.Python之禅(The Zen of Python)

Python之禅 最早由 Tim Peters在Python邮件列表中发表,它包含了影响Python编程语言设计的19条软件编写原则。在最初及后来的一些版本中,一共包含20条,其中第20条是“这一条留空(…)请 Guido 来填写”。这留空的一条从未公布也可能并不存在。这些文本属于 公共领域。

Python之禅作为一个信息条款被录入Python增强建议(PEP)的第20条,在Python语言的官方网站也能找到。它还作为复活节彩蛋被包含在Python解释器中。如果输入 import this 就会在Python的编程环境IDLE中显示。

Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 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!
优美优于丑陋,

明了优于隐晦;

简单优于复杂,

复杂优于凌乱,

扁平优于嵌套,

稀疏优于稠密,

可读性很重要!

即使实用比纯粹更优,

特例亦不可违背原则。

错误绝不能悄悄忽略,

除非它明确需要如此。

面对不确定性,

拒绝妄加猜测。

任何问题应有一种,

且最好只有一种,

显而易见的解决方法。

尽管这方法一开始并非如此直观,

除非你是荷兰人。

做优于不做,

然而不假思索还不如不做。

很难解释的,必然是坏方法。

很好解释的,可能是好方法。

命名空间是个绝妙的主意,

我们应好好利用它。

原创文章,作者:huoxiaoqiang,如若转载,请注明出处:https://www.huoxiaoqiang.com/python/pythonhigh/2649.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注