Python之禪

2018-05-09_220820.jpg
凡是用過 Python的人,基本上都知道在交互式解釋器中輸入 import this 就會顯示 Tim Peters 的 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!
翻譯和解釋
Beautiful is better than ugly.
# 優(yōu)美勝于丑陋(Python以編寫優(yōu)美的代碼為目標)
Explicit is better than implicit.
# 明了勝于晦澀(優(yōu)美的代碼應(yīng)當是明了的,命名規(guī)范,風(fēng)格相似)
Simple is better than complex.
# 簡潔勝于復(fù)雜(優(yōu)美的代碼應(yīng)當是簡潔的,不要有復(fù)雜的內(nèi)部實現(xiàn))
Complex is better than complicated.
# 復(fù)雜勝于凌亂(如果復(fù)雜不可避免,那代碼間也不能有難懂的關(guān)系,要保持接口簡潔)
Flat is better than nested.
# 扁平勝于嵌套(優(yōu)美的代碼應(yīng)當是扁平的,不能有太多的嵌套)
Sparse is better than dense.
# 間隔勝于緊湊(優(yōu)美的代碼有適當?shù)拈g隔,不要奢望一行代碼解決問題)
Readability counts.
# 可讀性很重要(優(yōu)美的代碼是可讀的)
Special cases aren't special enough to break the rules.
Although practicality beats purity.
# 即便假借特例的實用性之名,也不可違背這些規(guī)則(這些規(guī)則至高無上)
Errors should never pass silently.
Unless explicitly silenced.
# 不要包容所有錯誤,除非你確定需要這樣做(精準地捕獲異常,不寫except:pass風(fēng)格的代碼)
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是指Guido)
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!
# 命名空間是一種絕妙的理念,我們應(yīng)當多加利用(倡導(dǎo)與號召)
比較惡搞的是,其實 this 模塊的代碼完全違背了這些原則,為了方便你查看它的代碼,我把它貼出來:
s = """Gur Mra bs Clguba, ol Gvz Crgref
Ornhgvshy vf orggre guna htyl.
Rkcyvpvg vf orggre guna vzcyvpvg.
Fvzcyr vf orggre guna pbzcyrk.
Pbzcyrk vf orggre guna pbzcyvpngrq.
Syng vf orggre guna arfgrq.
Fcnefr vf orggre guna qrafr.
Ernqnovyvgl pbhagf.
Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
Nygubhtu cenpgvpnyvgl orngf chevgl.
Reebef fubhyq arire cnff fvyragyl.
Hayrff rkcyvpvgyl fvyraprq.
Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.
Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.
Abj vf orggre guna arire.
Nygubhtu arire vf bsgra orggre guna *evtug* abj.
Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.
Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!"""
d = {}
for c in (65, 97):
for i in range(26):
d[chr(i+c)] = chr((i+13) % 26 + c)
print "".join([d.get(c, c) for c in s])
這段晦澀、復(fù)雜、凌亂的代碼,莫非是 Tim Peters 提供的反例?