PyTips 0x00 - Python 之禪與 Pythonic

Python 之禪與 Pythonic

Python 之禪是 Python 語言的設計哲學與所倡導的編程理念,Pythonic 則是指基於 Python 理念編寫更加符合 Python 語法習慣(idiomatic Python)的代碼,這也是本項目所追求的目標,所以以本篇做爲開頭。html

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 之禪,by Tim Peterspython

優美勝於醜陋編程

明確勝於隱晦less

簡單勝於複雜ide

複雜勝於凌亂post

扁平勝於嵌套ui

稀疏勝於緊湊this

可讀性相當重要idea

即使特例,也需服從以上規則spa

除非刻意追求,錯誤不該跳過

面對歧義條件,拒絕嘗試猜想

解決問題的最優方法應該有且只有一個

儘管這一方法並不是顯而易見(除非你是Python之父)

動手勝於空想

空想勝於不想

難以解釋的實現方案,不是好方案

易於解釋的實現方案,纔是好方案

命名空間是個絕妙的理念,多多益善!

參考

  1. 《Python之禪》的翻譯和解釋

  2. What is Pythonic?

相關文章
相關標籤/搜索