Python之禅

起因

一次技术群里大佬们谈起了新手常常写的丑陋代码,提及Python之禅,我一看发现我也没看过这玩意.故,在此学习一下.

原文

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.

稀疏优于稠密,代码中操作稠密,例如++i/–i等运算符来压缩代码行,将会产生难以阅读和容易出错的代码

Readability counts.

可读性,意义重大.

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

特殊的case也不能打破规则

Although practicality beats purity.

尽管我们在纯粹性和实用性之间是倾向实用性.

Errors should never pass silently.

绝不静悄悄地放过任何error

Unless explicitly silenced.

除非明确说明应该安静地pass掉error

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之父(Guido).

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.

如果这个实现很容易解释,那么这可能是个很好的idea.

Namespaces are one honking great idea – let’s do more of those!

命名空间的思维非常优秀(honking 鸣笛 - - 什么意思),多用用.

希望自己多回头看看这篇笔记.