Python之禅——跟老吕学Python编程

Python之禅------跟老吕学Python编程

  • Python之禅
    • [1.**Beautiful is better than ugly.**](#1.Beautiful is better than ugly.)
    • [2.**Explicit is better than implicit.**](#2.Explicit is better than implicit.)
    • [3.**Simple is better than complex.**](#3.Simple is better than complex.)
    • [4.**Complex is better than complicated.**](#4.Complex is better than complicated.)
    • [5.**Flat is better than nested.**](#5.Flat is better than nested.)
    • [6.**Spare is better than dense.**](#6.Spare is better than dense.)
    • [7.**Readability counts.**](#7.Readability counts.)
    • [8.**Special cases aren't special enough to break the rules.**](#8.Special cases aren’t special enough to break the rules.)
    • [9.**Although practicality beats purity.**](#9.Although practicality beats purity.)
    • [10.**Errors should never pass silently.**](#10.Errors should never pass silently.)
    • [11.**Unless explicitly silenced.**](#11.Unless explicitly silenced.)
    • [12.**In the face of ambiguity , refuse the temptation to guess.**](#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.**](#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.**](#14.Although that way may not be obvious at first unless you’re Dutch.)
    • [15.**Now is better than never.**](#15.Now is better than never.)
    • [16.**Although never is often better than right now.**](#16.Although never is often better than right now.)
    • [17.**If the implementation is hard to explain , it's a bad idea.**](#17.If the implementation is hard to explain , it’s a bad idea.)
    • [18.**If the implementation is easy to explain , it's a good idea.**](#18.If the implementation is easy to explain , it’s a good idea.)
    • [19.**Namespaces are one honking great idea -- let's do more of those !**](#19.Namespaces are one honking great idea – let’s do more of those !)

Python之禅


在Python解释器中输入"import this"会发生什么?

在Python IDLE Shell窗口中输入Python代码 import this 会有一段 "Python之禅" 格言,作者是内核开发者Tim PetersGuido 叔的 Python 语言设计指导原则浓缩为了 19 条开发哲学

源代码如下:

python 复制代码
import this

展示Tim PetersThe Zen of Python "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.
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的理念都包含在Tim Peters撰写的 Python之禅 ------ "import this" 中的指导原则对于我们是很重要的,能给予我们很大的帮助。

1.Beautiful is better than ugly.

优美胜于丑陋

Python 以编写优美的代码为目标

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.Spare 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.

万事开头难,因为你不是Python之父(这里的Dutch是指Guido)

学Python一开始都是困难的,而你又不是Python之父(这里的Dutch是指Python之父),坚持学习才能够学好Python。

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.

如果方案难以被解释清楚,那么一定是个烂方案

Python编程是简单而且直接的,如果你都没想明白整个程序的合理编写思路,那么还不如不要动手去写了。

18.If the implementation is easy to explain , it's a good idea.

如果方案实现很容易解释,那么一定是个好方案

Python编程是简单而且直接的,如果你要编写的整个程序的思路清晰合理,那么你将会把这个方案做的很好。

19.Namespaces are one honking great idea -- let's do more of those !

命名空间是一种绝妙的理念,我们应当多加利用(倡导与号召)

命名空间是一个好的理念,善于运用

如果您觉得本文有帮助,辛苦您点个赞、收个藏、或评论一句,您的举手之劳将对我提供了无限的写作动力!

博主ID:Python老吕,希望大家点赞、评论、收藏。


相关推荐
smppbzyc7 分钟前
2025年亚太杯(中文赛项)数学建模B题【疾病的预测与大数据分析】原创论文讲解(含完整python代码)
python·数学建模·数据分析·数学建模竞赛·亚太杯数学建模·亚太杯
nbsaas-boot8 分钟前
Go语言生态成熟度分析:为何Go还无法像Java那样实现注解式框架?
java·开发语言·golang
xiaocainiao88119 分钟前
Python 实战:构建可扩展的命令行插件引擎
开发语言·python
碧海蓝天202242 分钟前
C++法则21:避免将#include放在命名空间内部。
开发语言·c++
兮动人1 小时前
Java应用全链路故障排查实战指南:从系统资源到JVM深度诊断
java·开发语言·jvm
R-sz1 小时前
导出word并且插入图片
开发语言·c#·word
CodeWithMe1 小时前
【读书笔记】《C++ Software Design》第一章《The Art of Software Design》
开发语言·c++
脑袋大大的1 小时前
判断当前是否为钉钉环境
开发语言·前端·javascript·钉钉·企业应用开发
运器1231 小时前
【一起来学AI大模型】PyTorch DataLoader 实战指南
大数据·人工智能·pytorch·python·深度学习·ai·ai编程
音元系统1 小时前
Copilot 在 VS Code 中的免费替代方案
python·github·copilot