Python的設計哲學探究,python哲學探究

來源:互聯網
上載者:User

Python的設計哲學探究,python哲學探究

在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]: thisOut[3]: <module 'this' from '/usr/lib/python2.7/this.py'>In [4]: type(this)Out[4]: moduleIn [5]: from this import c  d  i  s  

可以看出來是一個模組,裡邊有這幾個變數,同時我們可以根據路徑去看this.py的內容,如下:

s = """Gur Mra bs Clguba, ol Gvz CrgrefOrnhgvshy 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 dIn [6]: dOut[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 PetersPython 的設計哲學,作者:Tim PetersBeautiful 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!就像命名空間就是一個絕妙的想法,應當多加利用。

 

 

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.