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老吕,希望大家点赞、评论、收藏。


相关推荐
wn53122 分钟前
【Go - 类型断言】
服务器·开发语言·后端·golang
Narutolxy32 分钟前
Python 单元测试:深入理解与实战应用20240919
python·单元测试·log4j
Hello-Mr.Wang34 分钟前
vue3中开发引导页的方法
开发语言·前端·javascript
救救孩子把37 分钟前
Java基础之IO流
java·开发语言
WG_1738 分钟前
C++多态
开发语言·c++·面试
宇卿.1 小时前
Java键盘输入语句
java·开发语言
Amo Xiang1 小时前
2024 Python3.10 系统入门+进阶(十五):文件及目录操作
开发语言·python
liangbm31 小时前
数学建模笔记——动态规划
笔记·python·算法·数学建模·动态规划·背包问题·优化问题
friklogff1 小时前
【C#生态园】提升C#开发效率:深入了解自然语言处理库与工具
开发语言·c#·区块链
B站计算机毕业设计超人1 小时前
计算机毕业设计Python+Flask微博情感分析 微博舆情预测 微博爬虫 微博大数据 舆情分析系统 大数据毕业设计 NLP文本分类 机器学习 深度学习 AI
爬虫·python·深度学习·算法·机器学习·自然语言处理·数据可视化