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 tha ...
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!
可能你已經猜出來了,沒錯,每個 Pythoneer 都知道的彩蛋,在互動式命令行輸入 import this,會有一段 Python 之禪格言,作者是內核開發者 Tim Peters, Guido 叔的 Python 語言設計指導原則濃縮為了 19 條開發哲學。
譯文:
美麗優於醜陋。(Python 以編寫優美的代碼為目標)
清楚優於含糊。(優美的代碼應當是明瞭的,命名規範,風格相似)
簡單優於複雜。(優美的代碼應當是簡潔的,不要有複雜的內部實現)
複雜優於繁瑣。(如果複雜不可避免,那代碼間也不能有難懂的關係,要保持介面簡潔)
平坦優於曲折。(優美的代碼應當是扁平的,不能有太多的嵌套)
寬鬆優於密集。(優美的代碼有適當的間隔,不要奢望一行代碼解決問題)
重要的是可讀性。(優美的代碼是可讀的)
特殊的案例不足以特殊到破壞規則。(這些規則至高無上)
儘管實踐可以打破真理。
錯誤卻不可置之不理。
除非另有明確要求。(精準地捕獲異常,不寫 except:pass 風格的代碼)
面對模棱兩可,拒絕猜測。
總會有一個 —— 最好是只有一個 —— 顯而易見的方式來明辨。(如果不確定,就用窮舉法)
哪怕這種方式在開始的時候可能並不明顯 —— 除非你是荷蘭人 。(作者Guido von Rossum是荷蘭人)
現在有比沒有好。
儘管沒有經常好於現在。(做也許好過不做,但不假思索就動手還不如不做,動手之前要細思量)
如果如何實現很難被解釋清楚,那麼這個想法就是一個壞想法。
如果如何實現可以被很好的解釋,那麼這是一個好想法。(方案測評標準)
命名空間就是一個非常好的想法 —— 讓我們在這方面多做些工作吧!(倡導與號召)