欢迎访问我的博客,你的支持,是我最大的动力!

Python模块--this模块 Python之禅

Python 小马奔腾 15163℃ 评论
目录:
[显示]

Python有一个模块很特别,名字叫this,说它特别是因为导入这个模块,会显示一段话,而这段话的内容也被称之为Python之禅。

this这个模块通常在交互式解释器中使用,因为该模块并没有什么实际的功能,只是输出一段话。

this模块源码

 

 

上面的源码来自Python3,Python2也类似,仅最后一句print有所区别。

代码说明

该代码块首先定义了一个字符串s,这也是最终输出到屏幕的内容,但是很显然,它并不是最终的内容,而是通过某种方式加了密,因此显得非常凌乱,不能直接看出来其内容。

接下来的几行是转换函数。定义了一个空字典用于存放最终的字符映射关系。只要知道chr(65)='A' chr(97)='a'就能明白,其实这里是分别让大写和小写的26个字母分别向后移动13。

最后的输入语句,从字符串s中依次取每个字符,以取出的字符为key从字典d中得到对映的字符,如果字符不是字母,则保持字符不变(其实也就是不对标点符号)做映射。将所有的字符连接起来,就是最后得到的内容。

this模块执行效果

 

>>> 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!
>>>this.d
{'A': 'N', 'C': 'P', 'B': 'O', 'E': 'R', 'D': 'Q', 'G': 'T', 'F': 'S', 'I': 'V', 'H': 'U', 'K': 'X', 'J': 'W', 'M': 'Z', 'L': 'Y', 'O': 'B', 'N': 'A', 'Q': 'D', 'P': 'C', 'S': 'F', 'R': 'E', 'U': 'H', 'T': 'G', 'W': 'J', 'V': 'I', 'Y': 'L', 'X': 'K', 'Z': 'M', 'a': 'n', 'c': 'p', 'b': 'o', 'e': 'r', 'd': 'q', 'g': 't', 'f': 's', 'i': 'v', 'h': 'u', 'k': 'x', 'j': 'w', 'm': 'z', 'l': 'y', 'o': 'b', 'n': 'a', 'q': 'd', 'p': 'c', 's': 'f', 'r': 'e', 'u': 'h', 't': 'g', 'w': 'j', 'v': 'i', 'y': 'l', 'x': 'k', 'z': 'm'}
>>>

Pthon之禅

The Zen of Python, by Tim Peters(核心开发者)
--Python之禅

Beautiful is better than ugly丑陋的.
--优美胜于丑陋(Python 以编写优美的代码为目标)
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静静地.
--也不该默默忽略错误(不写 except:pass 风格的代码)
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荷兰人(这里的Dutch是指 Guido).
--虽然并不容易,因为你不是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模块--this模块 Python之禅

喜欢 (0)or分享 (0)