跳转至

Python口头禅

《Python之禅》是2004年的Python改进提案,编号为PEP 20。禅诗、偈颂的形式。黄复雄译。

摘要#

老资格的Python人Tim Peters将仁慈独裁者(BDFL)1的Python设计指导原则简明扼要地归纳成20条警句,只有其中19句被记载下来。

Python口头禅 / The Zen of Python#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
美好过丑。 / 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.
不过开始的时候它可能并非显然——除非你是荷兰人[^Rossum]。 / 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!

  1. 指Python创始人,荷兰人吉多·范罗苏姆(Guido van Rossum)。 

评论