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


相关推荐
lzb_kkk6 分钟前
【JavaEE】JUC的常见类
java·开发语言·java-ee
SEEONTIME6 分钟前
python-24-一篇文章彻底掌握Python HTTP库Requests
开发语言·python·http·http库requests
Bearnaise6 分钟前
PointMamba: A Simple State Space Model for Point Cloud Analysis——点云论文阅读(10)
论文阅读·笔记·python·深度学习·机器学习·计算机视觉·3d
起名字真南24 分钟前
【OJ题解】C++实现字符串大数相乘:无BigInteger库的字符串乘积解决方案
开发语言·c++·leetcode
tyler_download36 分钟前
golang 实现比特币内核:实现基于椭圆曲线的数字签名和验证
开发语言·数据库·golang
小小小~36 分钟前
qt5将程序打包并使用
开发语言·qt
hlsd#37 分钟前
go mod 依赖管理
开发语言·后端·golang
哇咔咔哇咔37 分钟前
【科普】conda、virtualenv, venv分别是什么?它们之间有什么区别?
python·conda·virtualenv
小春学渗透38 分钟前
Day107:代码审计-PHP模型开发篇&MVC层&RCE执行&文件对比法&1day分析&0day验证
开发语言·安全·web安全·php·mvc
杜杜的man41 分钟前
【go从零单排】迭代器(Iterators)
开发语言·算法·golang