python哲学之道

最近在找python相关学习资料的时候,发现了一个好玩的东西。

进入 Python 安装目录:Python/Lib/idlelib/,运行 idle.bat,然后输入 import this,你会看到下面的一段话,也尝试着翻译了一下:

The Zen of Python, by Tim Peters

python 哲学之道 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.

这显然不容易,除非你是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!

命名空间是一个绝妙的主意,我们应多加利用。