跳到主要内容

Python 之禅

「Python 之禅」属于 Python 的 “八荣八耻” —— 每一个有追求的 Python 工程师都应该谨记于心。

>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
优美胜于丑陋(Python以编写优美的代码为目标)

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之父呢

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!
命名空间是个绝妙的想法,让我们多多使用它们吧!