天天看点

the zen of python_如何理解「The Zen of Python」?

import this

The Zen of Python, by Tim Peters

Python之禅 ,by Tim Peters

Beautiful is better than ugly.

优美好于丑陋(Python 以优美的代码为其风格,不要写丑陋的代码)

Explicit is better than implicit.

明了好于隐晦(Python的每一行代码、每一个变量、每一函数的意义应该是明确的,让人看了一目了然)

Simple is better than complex.

简洁好于复杂(Python的每一行代码应当是简洁的,一行代码只为一个子功能)

Complex is better than complicated.

复杂好于凌乱(如果无法避免引入复杂的代码,也要尽量保证代码之间明确简洁的关系)

Flat is better than nested.

扁平好于嵌套(Python的代码结构应该是扁平的,不应该有太多嵌套的结构)

Sparse is better than dense.

稀疏好于密集

(Python代码之间应该是有间隔的,每个功能块、每个函数、每个参数、每个类之间应当有适当的间距)

Readability counts.

代码可读性很重要

(Python代码具有很好的可读性,注释、变量名、函数名、类名、模块名、代码块功能等的作用和意义都是一目了然的)

Special cases aren't special enough to break the rules.

Although practicality beats purity.

虽然实用性很重要,但任何特殊情况都不足以特殊到违背上述规则

(不要为了处理某一个特殊情况,而破坏了上述任何一个规则)

Errors should never pass silently.

Unless explicitly silenced.

不要忽视任何错误,除非有意为之(任何时候都要对异常和错误进行处理,不要写 except:pass 风格的代码)

In the face of ambiguity, refuse the temptation to guess.

There should be one-- and preferably only one --obvious way to do it.

面对模棱两可的情况,拒绝享受让别人去猜测的乐趣

提供有且仅有的一种最明显解决方法

(解决一个问题的方法可能会有很多种,但在Python中,只选择最明显的那一个)

Although that way may not be obvious at first unless you're Dutch.

虽然起初这很难做到,除非你是荷兰人(荷兰人暗指 Python之父:Guido van Rossum(龟叔) )

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!

命名空间是一个很棒的注意,应当多加利用

Hello World!

你好,世界!

存货 共勉。