1. 程式人生 > >python:python之禪

python:python之禪

zha 說明 should 也不能 tac 開篇 special cnblogs names

最近在學python,今晚看了一個名叫“python全棧之路系列”博客的關於python的相關博客,其中開篇就說到了python的設計哲學:優雅,簡潔,高效。。。

可以在編譯器裏面輸入如下語句來查看python語言的設計哲學:

 1 >>> import this
 2 The Zen of Python, by Tim Peters
 3 
 4 Beautiful is better than ugly.
 5 Explicit is better than implicit.
 6 Simple is better than complex.
 7 Complex is
better than complicated. 8 Flat is better than nested. 9 Sparse is better than dense. 10 Readability counts. 11 Special cases arent special enough to break the rules. 12 Although practicality beats purity. 13 Errors should never pass silently. 14 Unless explicitly silenced. 15 In the face of ambiguity, refuse the temptation to guess.
16 There should be one-- and preferably only one --obvious way to do it. 17 Although that way may not be obvious at first unless youre Dutch. 18 Now is better than never. 19 Although never is often better than *right* now. 20 If the implementation is hard to explain, its a bad idea. 21 If the implementation is
easy to explain, it may be a good idea. 22 Namespaces are one honking great idea -- lets do more of those!

中英文釋義如下:

 1 Beautiful is better than ugly.
 2 # 優美勝於醜陋(Python以編寫優美的代碼為目標)
 3 Explicit is better than implicit.
 4 # 明了勝於晦澀(優美的代碼應當是明了的,命名規範,風格相似) 
 5 Simple is better than complex.
 6 # 簡潔勝於復雜(優美的代碼應當是簡潔的,不要有復雜的內部實現) 
 7 Complex is better than complicated.
 8 # 復雜勝於淩亂(如果復雜不可避免,那代碼間也不能有難懂的關系,要保持接口簡潔)
 9 Flat is better than nested.
10 # 扁平勝於嵌套(優美的代碼應當是扁平的,不能有太多的嵌套) 
11 Sparse is better than dense.
12 # 間隔勝於緊湊(優美的代碼有適當的間隔,不要奢望一行代碼解決問題) 
13 Readability counts.
14 # 可讀性很重要(優美的代碼是可讀的) 
15 Special cases arent special enough to break the rules.
16 Although practicality beats purity.
17 # 即便假借特例的實用性之名,也不可違背這些規則(這些規則至高無上) 
18 Errors should never pass silently.
19 Unless explicitly silenced.
20 # 不要包容所有錯誤,除非你確定需要這樣做(精準地捕獲異常,不寫except:pass風格的代碼) 
21 In the face of ambiguity, refuse the temptation to guess.
22 # 當存在多種可能,不要嘗試去猜測 
23 There should be one-- and preferably only one --obvious way to do it.
24 # 而是盡量找一種,最好是唯一一種明顯的解決方案(如果不確定,就用窮舉法) 
25 Although that way may not be obvious at first unless youre Dutch.
26 # 雖然這並不容易,因為你不是 Python 之父(這裏的Dutch是指Guido)
27 Now is better than never.
28 Although never is often better than *right* now.
29 # 做也許好過不做,但不假思索就動手還不如不做(動手之前要細思量)
30 If the implementation is hard to explain, its a bad idea.
31 If the implementation is easy to explain, it may be a good idea.
32 # 如果你無法向人描述你的方案,那肯定不是一個好方案;反之亦然(方案測評標準) 
33 Namespaces are one honking great idea -- lets do more of those!
34 # 命名空間是一種絕妙的理念,我們應當多加利用(倡導與號召)

然後,我嘗試了裏面的一段demo代碼:

1 # coding = utf-82 print ("""
3 my name is zhangweigong
4 my blogs url is:www.cnblogs.com/imyalost
5 life is so short,I need python
6 """)

運行結果是可以成功運行的,但打印出來的結果前面,專門提及了python的設計哲學,說明,這段代碼是不夠簡潔高效的。。。

突然明白一個道理:學習一門編程語言,一定要了解這門語言的特性。。。

其實做一件事學一門技術也一樣(不限於編程語言,雖然編程語言也是一門技術、手段),要了解它的特定,才能更好的使用它,發揮它的作用!!!

引以為戒啊,少年。。。

python:python之禪