Python 之禅
创始人
2024-03-02 06:48:34
0

Python 之禅 ( Zen of Python ) 最早由 Tim Peters 于 1999 年发表于 Python 邮件列表中,它包含了影响 Python 编程语言设计的 19 条软件编写原则。在最初及后来的一些版本中,一共包含 20 条,其中第 20 条是“这一条留空(…)请 Guido 来填写”。这留空的一条从未公布也可能并不存在。

Python 之禅作为一个信息条款也被录入 Python 增强建议 ( Python Enhancement Proposals ) (PEP)的第 20 条,在 Python 语言的官方网站也能找到

此外,关于 Python 之禅,还有一件趣事。在 2001 召开第十届国际 Python 峰会(IPC 10,Pycon 的前身)前夕,会议主办方希望定制一件 T 恤,并绞尽脑汁地从投稿的标语中选择了一条 “import this”。然后,他们决定将这个语句实现在 Python 解释器中,于是将 Python 之禅的内容进行简单加密后放入到了 Python 2.2.1 中的 this.py 库当中。如果你在 Python 的解释器中输入 import this ,就会显示出来:

>>> import this;
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!

作为 Python 之禅系列文章的总结,我在下面重新整理并链接了之前的各篇文章。对于 Python 之禅的理解大家各有不同,目前也有几个不同的中文翻译版本。为了避免雷同,我们在翻译这系列文章时有意没有参考现有的 Python 之禅中文译本。因此,这里是我们自行翻译选定的译本,可能在理解上有不到位的地方,也可能文字润色不够精要,大家也可以参考其他的译本形成你的理解和润色版本。

  1. 美观胜于丑陋 ( Beautiful is better than ugly. )
  2. 明确胜于隐晦 ( Explicit is better than implicit. )
  3. 简单胜过复杂 ( Simple is better than complex. )
  4. 复杂胜过错综复杂 ( Complex is better than complicated. )
  5. 扁平胜过嵌套 ( Flat is better than nested. )
  6. 稀疏胜过密集 ( Sparse is better than dense. )
  7. 可读性很重要 ( Readability counts. )
  8. 特殊情况不足以违反规则 ( Special cases aren’t special enough to break the rules. )
  9. 虽然,实用性胜过纯洁性 ( Although practicality beats purity. )
  10. 错误绝不应该悄悄传递 ( Errors should never pass silently. )
  11. 除非显式消除 ( Unless explicitly silenced. )
  12. 面对歧义 ( In the face of ambiguity, ) 要拒绝猜测的诱惑 ( refuse the temptation to guess. )
  13. 尽量找一种 ( There should be one - ) 最好是唯一一种明显的解决方案 ( and preferably only one - obvious way to do it. )
  14. 虽然这种方式一开始可能并不明显 ( Although that way may not be obvious at first. ) (除非你是荷兰人) ( unless you’re Dutch. )
  15. 现在有总比永远没有好 ( Now is better than never. )
  16. 虽然将来总比现在好 ( Although never is often better than right now. )
  17. 如果一个实现难以解释 ( If the implementation is hard to explain, ) 那就是个坏思路 ( it’s a bad idea. )
  18. 如果一个实现易于解释 ( If the implementation is easy to explain, ) 那它可能是一个好思路 ( it may be a good idea. )
  19. 命名空间是一个非常棒的想法 ( Namespaces are one honking great idea— ) 让我们做更多的命名空间! ( let’s do more of those! )

相关内容

AI+Python数据分析...
教学大纲 01 课程性质和目的 数据分析与可视化为数据科学与大数据...
2026-05-06 20:23:25
什么是miniQMT,相比...
什么是miniQMT,相比大QMT有什么特点? 迅投 miniQM...
2026-01-22 09:51:16
Java、Python等开...
IT之家 9 月 24 日消息,开源安全基金会(OpenSSF)昨...
2025-09-24 14:25:34
python基础语法【迭代...
1.迭代器 1.什么是迭代器(iter)...
2025-06-01 20:30:55
手把手教你使用Flask框...
目录前言0、Flask框架的详细介绍一、Flask 框架封装接口1...
2025-06-01 17:11:00
【练习题】python函数...
写一个匿名函数,判断指定的年是否是闰年 (先直接用普...
2025-06-01 15:13:03

热门资讯

“Linux 中国” 开源社区... 这是一个艰难的决定 —— 这不是调侃,而是当你真正做出改变了你十几年生活轨迹的决定时,你就会明白这个...
硬核观察 #1263 德国铁路... #1 德国铁路公司招聘 Windows for Workgroups 3.11 管理员该职位要求了解...
人工智能教程(七):Sciki... 在本系列的 上一篇文章 中,我们用 TensorFlow 构建了第一个神经网络,然后还通过 Kera...
关闭 WSL 中正在运行的 L... 以下是使用 WSL 关闭在 Windows 内运行的 Linux 系统的不同方法。你使用 WSL 在...
MX Linux 23.2 “... 最新的 MX Linux 23.2 小版本更新带来了基本的改进和新增功能。MX Linux 推出了 ...
硬核观察 #1262 网络上最... #1 网络上最古老的软件存档库之一即将关闭美国新墨西哥州立大学(NMSU)最近宣布,即将于 2024...
如何在 RHEL 上通过绑定配... 在 Linux 中,你可以根据自己的需要创建高级网络设置,如 网络绑定 Network Bondin...
Linux Matrix 消息... Matrix 用户可以升级 Fractal 6 了!Fractal 是目前 最好的 Matrix 客...
如何通过 VLC 使用字幕 使用 VLC 媒体播放器播放和管理字幕的新手指南。我是一个超级动漫迷,如果你想观看最新的剧集,那么你...
硬核观察 #1261 X 计划... #1 X 计划在奥斯汀建立内容审核总部据报道,该部门将招聘 100 名全职员工,将主要关注 CSE ...