python之禪

起源

2001年秋召開第十屆International Python Conference(IPC 10,Pycon的前身),打算徵集一條印在會議T恤衫上的標語,最終他們從Python社區收到了500多條投稿。並最中篩選確認爲import this ,他們選擇之後覺得需要實現它,經過討論最中定義爲了由Tim Peter 寫的《The zen of Python》

觸發

在交互命令中輸入import this 就會觸發。

>>> 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!

譯文

# 譯文 轉載
Beautiful is better than ugly.
# 優美勝於醜陋(Python以編寫優美的代碼爲目標)

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.
# 所有錯誤都不應該被直接忽略,除非能夠被精確的捕獲之後。(其中一個典型的例子就是,不建議用Exception:pass來直接忽略所有異常。)

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. 
# 雖然起初這並不那麼顯而易見,因爲你不是 Python 之父(這裏的Dutch是指Python之父Guido Van Rossum,他是荷蘭人。)

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! 
# 命名空間是一種絕妙的理念,我們應當多加利用(倡導與號召)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章