在Python shell中輸入import this就會在屏幕上列印出來Python的設計哲學,如下: 大概意思大家可以看一下,也可以大概的理解Python為什麼是這樣的,同時,我們寫的代碼應該是什麼樣的了。 今天突然好奇是怎麼實現的,於是就探究了一下,先看this是啥: 可以看出來是一個模塊,里 ...
在Python shell中輸入import this就會在屏幕上列印出來Python的設計哲學,如下:
In [25]: 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!
大概意思大家可以看一下,也可以大概的理解Python為什麼是這樣的,同時,我們寫的代碼應該是什麼樣的了。
今天突然好奇是怎麼實現的,於是就探究了一下,先看this是啥:
In [3]: this Out[3]: <module 'this' from '/usr/lib/python2.7/this.py'> In [4]: type(this) Out[4]: module In [5]: from this import c d i s
可以看出來是一個模塊,裡邊有這幾個變數,同時我們可以根據路徑去看this.py的內容,如下:
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])
打開第一反應是一臉懵逼,還以為整的其他語言呢,一看下麵有一小段代碼,實際上是簡單的加密演算法,僅僅做了簡單的位移,類似於維吉尼亞加密~然後可以在終端試一下dir(this),如下:
In [5]: from this import c d i s In [5]: from this import d In [6]: d Out[6]: {'A': 'N', 'B': 'O', 'C': 'P', 'D': 'Q', 'E': 'R', 'F': 'S',
然後為什麼引入其他模塊沒有不會直接執行print呢?我猜是底層做了區分…這個需要看Python源碼,待我回頭探究一下回來補充(真的會回來麽
PS:
漢語版翻譯見(https://yijingping.github.io/2014/03/02/The-Zen-of-Python.html),此處摘抄如下:
The Zen of Python, by Tim Peters Python 的設計哲學,作者: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. 雖然這種方式開始時並不容易,除非你是 Python 之父。 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! 就像命名空間就是一個絕妙的想法,應當多加利用。