標籤:name import 命名 help beat cat cep style cas
本節點標題
1、python編程風格
1.1、python設計哲學
1.2、在Python解譯器內運行import this可以獲得完整的編程風格列表
=======================================================================================================================
1、python編程風格
1.1、python設計哲學
Python的設計哲學是“優雅”、“明確”、“簡單”。
Python開發人員的哲學是“用一種方法,最好是只有一種方法來做一件事”。
在設計Python語言時,如果面臨多種選擇,Python開發人員一般會拒絕花俏的文法,而選擇明確沒有或者很少有歧義的文法。這些準則被稱為“Python格言”。
1.2、在Python解譯器內運行import this可以獲得完整的編程風格列表
1優美勝於醜陋(Python 以編寫優美的代碼為目標)
2明了勝於晦澀(優美的代碼應當是明了的,命名規範,風格相似)
3簡潔勝於複雜(優美的代碼應當是簡潔的,不要有複雜的內部實現)
4複雜勝於淩亂(如果複雜不可避免,那代碼間也不能有難懂的關係,要保持介面簡潔)
5扁平勝於嵌套(優美的代碼應當是扁平的,不能有太多的嵌套)
6間隔勝於緊湊(優美的代碼有適當的間隔,不要奢望一行代碼解決問題)
7可讀性很重要(優美的代碼是可讀的)
8即便假借特例的實用性之名,也不可違背這些規則(這些規則至高無上)
9不要包容所有錯誤,除非你確定需要這樣做(精準地捕獲異常,不寫 except:pass 風格的代碼)
10當存在多種可能,不要嘗試去猜測
11而是盡量找一種,最好是唯一一種明顯的解決方案(如果不確定,就用窮舉法)
12雖然這並不容易,因為你不是 Python 之父(這裡的 Dutch 是指 Guido )
13做也許好過不做,但不假思索就動手還不如不做(動手之前要細思量)
14如果你無法向人描述你的方案,那肯定不是一個好方案;反之亦然(方案測評標準)
15命名空間是一種絕妙的理念,我們應當多加利用(倡導與號召)
[[email protected] ~]# python
Python 2.7.13 (default, Jan 29 2017, 23:09:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] 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!
>>>
python學習筆記(9)-python編程風格