Python補充06 Python之道

作者:Vamei 出處:http://www.cnblogs.com/vamei 歡迎轉載,也請保留這段聲明。謝謝!

Python有一個彩蛋,用下面語句調出:

import this

該彩蛋的文檔記錄於PEP 20

語句執行之後,終端將顯示一首"The Zen of Python"的詩,即“Python之道”。這首詩總結了Python的風格,可以指導Python程序員的編程。Python程序員會自豪宣稱自己"Pythonic"。Pythonic的一個基本標準,是寫出合乎“Python之道”的代碼。

下面是“Python之道”的全文,以及我附加的翻譯。

The Zen of Python, by Tim Peters Python之道 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的作者Guido是荷蘭人,這是在致敬) 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之道"強調美觀、簡單、可讀和實用,拒絕複雜或模糊。

歷史

Tim Peters於June 4, 1999的Python郵件列表,以"The Python Way"爲標題,發表了“Python之道”,得到許多Python程序員的認同。另一方面,2001年的International Python Conference #10 (IPC10, IPC是PyCon的前身)會議,主辦方希望在文化衫上印標語,而這一標語要能代表Python文化。到會的Python程序員們創作了500多條。組織者選了"import this"。後來,Python的作者Guido增加了this.py模塊,讓這個語句可以在解釋器中執行,打印“Python之道”。

Python社區很幽默。

此外,PyCon是Python愛好者的集會,可以去給別人展示自己的Python項目,或者聽講座。2012年的PyCon是在北京和上海同時舉行。

參考文章

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章