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 Peters,Guido 叔的 Python 语言设计指导原则浓缩为了 19 条开发哲学。

源代码如下:

python 复制代码
import this

展示Tim Peters的The 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老吕,希望大家点赞、评论、收藏。


相关推荐
Thneonl3 小时前
Celery 生产踩坑:1000 任务积压与 acks_late 双重执行
后端·python
清桔3 小时前
模型的调用
python
小小张说故事3 小时前
Python 多线程为什么跑不快?asyncio 入门指南:异步并发从零上手
后端·python
10年前端老司机3 小时前
干货分享|企业智能知识库 Rerank 重排序落地实践与踩坑总结
python·aigc·agent
柯南46683 小时前
【AI开发之Rust】第 18 课:SQLite 持久化与本地缓存 —— 给 store 填上真实现
rust·编程语言
天天被压力3 小时前
【Python 量化取数指南 #13】Python 把行情落库:sqlite 一键存,回测随用随取
java·人工智能·python
天天被压力3 小时前
【Python 量化取数指南 #14】Python 清洗行情数据:复权停牌对齐,回测不翻车
java·人工智能·python
Python私教3 小时前
Python环境配置:conda+PyCharm+换源,附6个坑
人工智能·python·pycharm
柯南46683 小时前
【AI开发之Rust】第 17 课:项目总览与核心架构 —— AI 助手 Rust 核心从 0 到 1
rust·编程语言
databook3 小时前
检测数据异常值的五种统计技术
python·数据挖掘·数据分析