Python之禅(Zen)

发布于 2024-02-06  71 次阅读


在IDE中输入import this会有python的小彩蛋

The Zen of Python, by 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.
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!

理解

Beautiful is better than ugly.
优雅的代码更胜于丑陋的代码。设计和编码应追求美观和整洁。

Explicit is better than implicit.
明确的代码比隐晦的代码更好。这是为什么 Python 需要明确使用 self 和其他语法。

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 的创始人 Guido van Rossum,他是荷兰人。它意味着解决方案可能不是一开始就明显的,除非你像 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.
如果实现方案容易解释,那么它可能是个好主意。

Namespaces are one honking great idea -- let's do more of those!
命名空间是个很好的概念,我们应该更多地使用它。这表示了 Python 的支持和鼓励模块化和组织代码。