1. 程式人生 > >一入python深似海--python之道

一入python深似海--python之道

ipp space 指導 eat pla cas sim cat esp

python社區不乏幽默。先來看“python之道”這首詩。

導入this包:

import this

輸出是一首詩,這首詩總結了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之道"強調美觀、簡單、可讀和有用,拒絕復雜或模糊。

一入python深似海--python之道