天天看點

Python 格言(The Zen of Python)

Python 的設計哲學是“優雅”、“明确”、“簡單”。是以,Perl 語言中“總是有多種方法來做同一件事”的理念在 Python 開發者中通常是難以忍受的。Python 開發者的哲學是“用一種方法,最好是隻有一種方法來做一件事”。在設計 Python 語言時,如果面臨多種選擇,Python 開發者一般會拒絕花俏的文法,而選擇明确的沒有或者很少有歧義的文法。由于這種設計觀念的差異,Python 源代碼通常被認為比 Perl 具備更好的可讀性,并且能夠支撐大規模的軟體開發。這些準則被稱為Python 格言。

在 Python 解釋器内運作 import this 可以獲得完整的清單,如圖所示:

Python 格言(The Zen of Python)

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!

原文連結:http://www.menglanglang.cn/blog-post/computer-technology/it-development/development-language/python/4749/