前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python小姿势 - Python之禅

Python小姿势 - Python之禅

作者头像
不吃西红柿
发布2023-04-28 16:16:31
1560
发布2023-04-28 16:16:31
举报
文章被收录于专栏:信息技术智库信息技术智库

Python之禅

Python之禅(The Zen of Python)是Python之父Guido van Rossum所著的一篇文章,总结了Python语言的哲学。

文章开头写道:

代码语言:javascript
复制
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.

“美丽胜于丑陋”。这句话的意思是说,我们应该尽量写出美丽的代码,而不是丑陋的代码。

这也是PEP20中所说的“There should be one-- and preferably only one --obvious way to do it”的另一种表达。

Explicit is better than implicit.

“明了胜于晦涩”。这句话的意思是说,我们应该尽量写出明了的代码,而不是晦涩难懂的代码。

这句话的含义与“Readability counts.”是一致的。

Simple is better than complex.

“简单胜于复杂”。这句话的意思是说,我们应该尽量写出简单的代码,而不是复杂的代码。

这句话的含义与“There should be one-- and preferably only one --obvious way to do it”是一致的。

Complex is better than complicated.

“复杂胜于繁琐”。这句话的意思是说,我们应该尽量写出复杂的代码,而不是繁琐的代码。

这句话的含义与“There should be one-- and preferably only one --obvious way to do it”是一致的。

Flat is better than nested.

“扁平胜于嵌套”。这句话的意思是说,我们应该尽量写出扁平的代码,而不是嵌套的代码。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-04-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档