作者: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是在北京和上海同時舉行。
文章列表