【Python】Python之禅

书上说:“Python程序员笃信代码可以编写得漂亮而优雅。”小新虽然学习了 Python这门神奇的语言,但是自认为自己仍处于一个菜鸟的水平。所以呢,暑假也没闲着,把自己的Python学习笔记赶紧整理成博客,勤能补拙嘛。
一个经验丰富的程序员肯定会提倡避繁就简的。Python社区的理念都包含在Tim Peters撰写的“Python之禅”中。所以我们今天来看一看这个Python之禅到底是什么东东。

Python之禅

要熟悉这个指导你怎么去编写优秀的代码的Python之禅,只需要在解释器中敲下执行命令:

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之禅的内容,但是暑假空余时间就比较多了,咱们就好好唠唠这个“短诗”。
首先,和平常的诗歌一样,交代题目、作者。

The Zen of Python, by Tim Peters
//Python之禅,Tim Peters

试问,正常人谁不喜欢长得漂亮的姑娘或者长得帅气的男生呢?反正小新是喜欢美而非丑的。在写代码时也是一样,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.
//即使假借特例的实用性之名,也不可违背这些规则

一个典型的例子:不建议使用Exception:pass来直接忽略所有异常。

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.
//虽然起初这并不那么显而易见,因为你不是Python之父

在动手之前要仔细的思量。

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之禅的全部理解了,如果你有什么更好地理解,请评论区里告诉小新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是初九

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值