Python的import this 惊喜彩蛋:Python之禅(The Zen of Python)

简介: Python的import this 惊喜彩蛋:Python之禅(The Zen of Python)

Python的import this 惊喜彩蛋:Python之禅(The Zen of Python)


Python解释器中输入“import this”会发生什么?


import this


下面在pycharm环境中的Python console窗口中输入: import this。如下所示


image.png

  • import 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!


The Zone Of Python

  • 下面是《The Zone Of Python》的中文翻译。这些准则不仅仅适用于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.
Unless explicitly silenced.
# 不要包容所有错误,除非你确定需要这样做(精准地捕获异常,不写except:pass风格的代码)
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 之父(这里的Dutch是指Guido)
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!
# 命名空间是一种绝妙的理念,我们应当多加利用(倡导与号召)



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.

Unless explicitly silenced.

不要包容所有错误,除非你确定需要这样做(精准地捕获异常,不写except:pass风格的代码)


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 之父(这里的Dutch是指Guido)


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!

命名空间是一种绝妙的理念,我们应当多加利用(倡导与号召)


按住Ctrl件,鼠标点击this,会出现如下所示的代码:


b5c80c882dfd4426b2eb03865b670862.png


d11371c80ef64d70b8bd7d9cb5e8949d.png

s = """Gur Mra bs Clguba, ol Gvz Crgref
Ornhgvshy vf orggre guna htyl.
Rkcyvpvg vf orggre guna vzcyvpvg.
Fvzcyr vf orggre guna pbzcyrk.
Pbzcyrk vf orggre guna pbzcyvpngrq.
Syng vf orggre guna arfgrq.
Fcnefr vf orggre guna qrafr.
Ernqnovyvgl pbhagf.
Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
Nygubhtu cenpgvpnyvgl orngf chevgl.
Reebef fubhyq arire cnff fvyragyl.
Hayrff rkcyvpvgyl fvyraprq.
Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.
Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.
Abj vf orggre guna arire.
Nygubhtu arire vf bsgra orggre guna *evtug* abj.
Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.
Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!"""
d = {}
for c in (65, 97):
    for i in range(26):
        d[chr(i+c)] = chr((i+13) % 26 + c)
print("".join([d.get(c, c) for c in s]))


我们可以看到这些字符我们是不认识的,而是经过相应的密码处理的。其实这里有一个故事:


2001年秋,Foretec(一家会议组织公司)正在准备召开第十届International Python Conference(IPC 10,Pycon的前身),Foretec打算征集一条印在会议T恤衫上的标语,最终他们从Python社区收到了500多条投稿。

Foretec邀请了Python的核心开发Guido, Fred, Jeremy,Tim Peter,Barry 等来担任评审。评审委员们从投稿中过滤出了130多条后就一直没有进展,直到会议快要召开时他们也没有确定最终标语。

会议就要开始的前几天,到了评审们不得不做出决定的时候了。于是由Tim Peter和Barry 两人轮流评审,每人每次淘汰一半留下一半,直到最后只剩一条。

最终,“import this”被选了出来,大家对"import this"非常满意。但是当选择了这条以后,他们意识到他们必须要实现它。经过简单的讨论后,“import this”的被定为输出Tim Peter写的《The Zen of Python》——Python编程和设计的指导原则。

Time Peter建议在代码中对输出的内容做一些简单的混淆,于是他们就用rot13对打印内容进行了一些处理。


其实,rot13就是凯撒密码的一种,每个字母变换为它后面的第13个字母。例如A变换为它后面的第13个字母N,所有字母变换关系如下图:


8cd898b2ccb5451dbb872fd19164a0d2.jpg


  • 下面引用这些代码,编写一个函数进行封装,如下所示
# python 之禅
def zenOfPython():
    s = """Gur Mra bs Clguba, ol Gvz Crgref
    Ornhgvshy vf orggre guna htyl.
    Rkcyvpvg vf orggre guna vzcyvpvg.
    Fvzcyr vf orggre guna pbzcyrk.
    Pbzcyrk vf orggre guna pbzcyvpngrq.
    Syng vf orggre guna arfgrq.
    Fcnefr vf orggre guna qrafr.
    Ernqnovyvgl pbhagf.
    Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
    Nygubhtu cenpgvpnyvgl orngf chevgl.
    Reebef fubhyq arire cnff fvyragyl.
    Hayrff rkcyvpvgyl fvyraprq.
    Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
    Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.
    Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.
    Abj vf orggre guna arire.
    Nygubhtu arire vf bsgra orggre guna *evtug* abj.
    Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.
    Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
    Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!"""
    d = {}
    for c in (65, 97):
        for i in range(26):
            d[chr(i+c)] = chr((i+13) % 26 + c)
    print("prython之禅》》》")
    print("".join([d.get(c, c) for c in s]))
if __name__ == '__main__':
    zenOfPython()


运行结果如下:

683f14d1e67a4696875ac5da773945a9.png

prython之禅》》》
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!

My favorite words


  • 下面给出我最喜欢的一句。


def myStr():
    myStr = "Clguba cebtenzzvat vf nggenpgvr naq hacerqvpnoyr."
    d = {}
    for c in (65, 97):
        for i in range(26):
            d[chr(i + c)] = chr((i + 13) % 26 + c)
    print("prython之禅》》》")
    print("".join([d.get(c, c) for c in myStr]))
if __name__ == '__main__':
    myStr()


试着运行看看会出什么结果吧!!!

  • 也可以将你对爱人最喜欢的一句话转换成Python之禅,然后送给他/她哦!!!嘿嘿嘿。


What is the zen of python


  • 人生中的烦恼都是自己找的,当心灵变得博大,空灵无物,犹如倒空了烦恼的杯子,便能恬淡安静

image.png


人的心灵,若能如莲花与日月,超然平淡,无分别心、取舍心、爱憎心、得失心,便能获得快乐与祥和。

水往低处流,云在天上飘,一切都自然和谐地发生,这就是平常心


image.png


拥有一颗平常心,人生如行云流水,回归本真,这便是参透人生,便是禅。

相关文章
|
1月前
|
数据可视化 数据挖掘 开发者
import有什么用,python中怎么使用import
import有什么用,python中怎么使用import
26 1
|
4月前
|
API Python
python 详细理解 import ---- openstack自动import class 到 特定命名空间
python 详细理解 import ---- openstack自动import class 到 特定命名空间
46 0
|
1月前
|
Python
[Python] 如何用import导入模块
[Python] 如何用import导入模块
23 0
|
3月前
|
C++ Python
python工程中import要点提炼
python工程中import要点提炼
|
3月前
|
Python
Python小姿势 - import random
Python小姿势 - import random
|
3月前
|
Python
Python小姿势 - import requests
Python小姿势 - import requests
|
4月前
|
开发者 Python
【python】tkinter组件,from Tkinter import * 与 import Tkinter 的区别
【python】tkinter组件,from Tkinter import * 与 import Tkinter 的区别
53 0
|
4月前
|
编译器 Python
Python 完美解决 Import "模块" could not be resolved ...
Python 完美解决 Import "模块" could not be resolved ...
48 0
|
4月前
|
XML 数据格式 Python
Python解释import、from ... import ... 和 from ... import *的区别。
解释import、from ... import ... 和 from ... import *的区别。
|
5月前
|
Linux 开发工具 C++
【vcpkg】像Python一样方便的import 自己的c++库
使用此种方式可无需设置 CMAKE_TOOLCHAIN_FILE 即可使用 vcpkg,且更容易完成配置工作。
197 0