The Zen of Python

简述

Python 中有一个彩蛋 – ​​The Zen of Python​​,被称作:

Python 之禅、Python 之道、Python 箴言 …

它总结了 Python 的风格,可以用来指导 Pythonista 的编程。

很多 Pythonista 会自豪地宣称自己为”Pythonic”,Pythonic 的一个基本标准就是写出合乎”Python 之禅”的代码 – 简练、明确、优雅……

|版权声明:一去、二三里,未经博主允许不得转载。

漫画版

The Zen of Python

看到这部动画片,一个词立刻浮现在脑海中–“完美”!

[En]

Seeing this cartoon, a word immediately came to mind-“perfect”!

The Zen of Python

在 Python shell 中输入 ​ ​import this​​​,就会展示 Tim Peters 的 ​ ​The Zen of Python​​:

>>> import thisThe Zen of Python, by Tim PetersBeautiful 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.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!

译文如下:

Python 之禅, by Tim Peters
优美胜于丑陋(Python 以编写优美的代码为目标)
清晰比晦涩好(漂亮的代码应该清晰,命名约定,相似的风格)

[En]

Clarity is better than obscurity (beautiful code should be clear, naming conventions, similar styles)

简单胜过复杂(漂亮的代码应该简洁,没有复杂的内部实现)

[En]

Simplicity is better than complexity (beautiful code should be concise, without complex internal implementations)

复杂胜过杂乱(如果复杂是不可避免的,代码之间不能有模糊的关系,保持界面简单)

[En]

Complexity is better than clutter (if complexity is inevitable, there can be no obscure relationship between the code, keep the interface simple)

扁平比嵌套好(漂亮的代码应该是扁平的,不要嵌套太多)

[En]

Flatness is better than nesting (beautiful code should be flat, not too much nesting)

间距比紧凑性好(优雅的代码有合适的间距,不要指望一行代码就能解决问题)

[En]

Spacing is better than compactness (graceful code has proper spacing, don’t expect a single line of code to solve the problem)

可读性很重要(漂亮的代码是可读的)

[En]

Readability is important (beautiful code is readable)

即使在特殊情况有用的幌子下,这些规则也不能被违反(这些规则是至高无上的)

[En]

Even under the guise of the usefulness of special cases, these rules cannot be violated (these rules are paramount)

不要包容所有错误,除非你确定需要这样做(精准地捕获异常,不写except:pass风格的代码)
当有多种可能性时,不要试图猜测

[En]

When there are multiple possibilities, don’t try to guess

取而代之的是,试着找到一个,最好是唯一明显的解决方案(如果你不确定,使用疲惫)

[En]

Instead, try to find one, preferably the only obvious solution (if you are not sure, use exhaustion)

虽然这并不容易,因为你不是 Python 之父(这里的 Dutch 是指 Guido)
做可能比不做要好,但不假思索地做会更好(三思而后行)

[En]

It may be better to do it than not to do it, but it is better to do it without thinking (think carefully before you do it)

如果你不能向别人描述你的计划,它肯定不是一个好的计划,反之亦然(方案评估标准)

[En]

If you can’t describe your plan to others, it’s certainly not a good plan, and vice versa (scheme evaluation criteria)

名称空间是一个很好的想法,我们应该更多地使用它们(倡导和呼吁)

[En]

Namespaces are a wonderful idea, and we should make more use of them (advocacy and call)

更多参考

Original: https://blog.51cto.com/waleon/5525591
Author: waleon
Title: The Zen of Python

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/504692/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球