Python 格言(The Zen of Python)
2020年08月30日 00:21:55 Python ⁄ 共 991字 暂无评论 ⁄ 被围观 3,371次

Python 的设计哲学是“优雅”、“明确”、“简单”。因此,Perl 语言中“总是有多种方法来做同一件事”的理念在 Python 开发者中通常是难以忍受的。Python 开发者的哲学是“用一种方法,最好是只有一种方法来做一件事”。在设计 Python 语言时,如果面临多种选择,Python 开发者一般会拒绝花俏的语法,而选择明确的没有或者很少有歧义的语法。由于这种设计观念的差异,Python 源代码通常被认为比 Perl 具备更好的可读性,并且能够支撑大规模的软件开发。这些准则被称为Python 格言。

在 Python 解释器内运行 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!

给我留言

留言无头像?