Python之禪(Pythonic)
“Pythonic”指的是以 Python 的方式去編寫程式碼、組織邏輯,及物件行為。
[root@cent7 ~]# python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 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!
>>> exit()
翻譯和解釋
Python之禪 by Tim Peters
優美勝於醜陋(Python 以編寫優美的程式碼為目標)
明瞭勝於晦澀(優美的程式碼應當是明瞭的,命名規範,風格相似)
簡潔勝於複雜(優美的程式碼應當是簡潔的,不要有複雜的內部實現)
複雜勝於凌亂(如果複雜不可避免,那程式碼間也不能有難懂的關係,要保持介面簡潔)
扁平勝於巢狀(優美的程式碼應當是扁平的,不能有太多的巢狀)
間隔勝於緊湊(優美的程式碼有適當的間隔,不要奢望一行程式碼解決問題)
可讀性很重要(優美的程式碼是可讀的)
即便假借特例的實用性之名,也不可違背這些規則(這些規則至高無上)
不要包容所有錯誤,除非你確定需要這樣做(精準地捕獲異常,不寫 except:pass 風格的程式碼)
當存在多種可能,不要嘗試去猜測
而是儘量找一種,最好是唯一一種明顯的解決方案(如果不確定,就用窮舉法)
雖然這並不容易,因為你不是 Python 之父(這裡的 Dutch 是指 Guido )
做也許好過不做,但不假思索就動手還不如不做(動手之前要細思量)
如果你無法向人描述你的方案,那肯定不是一個好方案;反之亦然(方案測評標準)
名稱空間是一種絕妙的理念,我們應當多加利用(倡導與號召)
http://www.cnblogs.com/skynet/archive/2013/05/06/3063245.html
[root@cent7 ~]# python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 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!
>>> exit()
翻譯和解釋
Python之禪 by Tim Peters
優美勝於醜陋(Python 以編寫優美的程式碼為目標)
明瞭勝於晦澀(優美的程式碼應當是明瞭的,命名規範,風格相似)
簡潔勝於複雜(優美的程式碼應當是簡潔的,不要有複雜的內部實現)
複雜勝於凌亂(如果複雜不可避免,那程式碼間也不能有難懂的關係,要保持介面簡潔)
扁平勝於巢狀(優美的程式碼應當是扁平的,不能有太多的巢狀)
間隔勝於緊湊(優美的程式碼有適當的間隔,不要奢望一行程式碼解決問題)
可讀性很重要(優美的程式碼是可讀的)
即便假借特例的實用性之名,也不可違背這些規則(這些規則至高無上)
不要包容所有錯誤,除非你確定需要這樣做(精準地捕獲異常,不寫 except:pass 風格的程式碼)
當存在多種可能,不要嘗試去猜測
而是儘量找一種,最好是唯一一種明顯的解決方案(如果不確定,就用窮舉法)
雖然這並不容易,因為你不是 Python 之父(這裡的 Dutch 是指 Guido )
做也許好過不做,但不假思索就動手還不如不做(動手之前要細思量)
如果你無法向人描述你的方案,那肯定不是一個好方案;反之亦然(方案測評標準)
名稱空間是一種絕妙的理念,我們應當多加利用(倡導與號召)
http://www.cnblogs.com/skynet/archive/2013/05/06/3063245.html
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/210154/viewspace-1437612/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python 之禪Python
- python之禪Python
- Python之禪-import this的實現PythonImport
- 快速開始HelloWorld和Python之禪Python
- 禪道PythonPython
- python入門與進階篇(八)之Pythonic與Python雜記Python
- python資料統計之禪道bug統計Python
- python推導式pythonic必備Python
- python進階(一)變數與資料型別、python之禪Python變數資料型別
- 設計模式之禪之代理模式設計模式
- 如何讓你的Python程式碼更加pythonic ?Python
- 替代 for 迴圈,讓 Python 程式碼更 pythonic !Python
- 批量系統設計之禪
- Python 禪道測試用例助手Python
- 幾個小技巧讓你的Python程式碼更PythonicPython
- 搓一個Pythonic listPython
- 玩轉設計模式——設計模式之禪設計模式
- pythonic context manager知多少PythonContext
- 字典物件的 Pythonic 用法(上)物件Python
- Pythonic AI generation of images and videosPythonAIIDE
- [譯] Erlang 之禪第二部分
- 以持續整合工具實現DevOps之禪dev
- 聊一聊 EventBus 原始碼和設計之禪原始碼
- 使用Python的enumerate()函式編寫更Pythonic的迴圈Python函式
- 悟禪機
- [譯] Erlang 之禪第一部分
- 陳星漢七年之癢:我不是“遊戲禪師”遊戲
- 讀秦小波《設計模式之禪》 -- 單例模式設計模式單例
- 讀秦小波《設計模式之禪》 -- 工廠模式設計模式
- 禪道命名標識約定-敏捷在禪道(五)敏捷
- Docker - 部署禪道Docker
- 10.25禪道
- 以禪悟禪,體驗AngularJs的優雅哲學AngularJS
- 禪道的使用:如何搭建Ubuntu環境安裝禪道Ubuntu
- 學會Lambda,讓程式Pythonic一點Python
- 幾種實用的 pythonic 語法Python
- 使用Docker部署禪道Docker
- 瞧瞧,這樣的「函式」才叫 Pythonic函式Python