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 tobreak the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse thetemptation to guess.

There should be one-- and preferably onlyone --obvious way to do it.

Although that way may not be obvious atfirst 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!

 

翻譯和解釋

Python之禪 by Tim Peter

優美勝於醜陋,(Python以編寫優美的代碼爲目標)

明瞭勝於晦澀,(優美的代碼應當是明瞭的,命名規範,風格相似)

簡潔勝於複雜,(優美的代碼應該是簡潔的,不要有複雜的內部實現)

複雜勝於凌亂,(如果複雜不可避免,那代碼間也不能有難懂的關係,要保持接口簡潔)

扁平勝於嵌套,(優美的代碼應該是扁平的,不能有太多的嵌套)

間隔勝於緊湊,(優美的代碼有適當的間隔,不要奢望一行代碼解決問題)

可讀性很重要,(優美的代碼是可讀的)

即便假借特例的實用性之名,也不可違背這些規則,(這些規則至高無上)

不要包容所有錯誤,除非你確定需要這麼做,(精準的捕獲異常,不屑exceptpass風格的代碼)

當存在多種可能,不要嘗試猜測,

而是儘量找一種,最好是唯一一種明顯的解決方案,(如果不確定,就用窮舉法)

雖然這並不容易,因爲你不是Python之父,

做也許好過不做,但是不假思索就動手還不如不做,(動手之前細思量)

如果你無法向人描述你的方案,那肯定不是一個好方案,反之亦然(方案測評標準)

命名空間是一種絕妙的理念,我們應當多加利用。(倡導與號召)


#翻譯和解釋是轉載別人的,英文版可以在編輯器中用"import this"來獲取

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章