文章目录

  • Python的import this 惊喜彩蛋:Python之禅(The Zen of Python)
    • import this
    • The Zone Of Python
    • My favorite words
    • What is the zen of python

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

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

import this

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

  • 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,会出现如下所示的代码:

s = """Gur Mra bs Clguba, ol Gvz CrgrefOrnhgvshy 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,所有字母变换关系如下图:

  • 下面引用这些代码,编写一个函数进行封装,如下所示

# python 之禅
def zenOfPython():s = """Gur Mra bs Clguba, ol Gvz CrgrefOrnhgvshy 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()

运行结果如下:

prython之禅》》》
The Zen of Python, by Tim PetersBeautiful 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

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

  • 人的心灵,若能如莲花与日月,超然平淡,无分别心、取舍心、爱憎心、得失心,便能获得快乐与祥和。
  • 水往低处流,云在天上飘,一切都自然和谐地发生,这就是平常心。
  • 拥有一颗平常心,人生如行云流水,回归本真,这便是参透人生,便是禅。

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

  1. Python中的惊喜彩蛋

    Python 作为一门开源的语言,它的社区为其贡献了一些十分幽默的东西. 当一门编程语言是开源的时候,往往会有产生一些搞笑和有趣的东西.通常,这意味着社区的贡献者会为该语言添加一些有趣和特别的彩蛋以及 ...

  2. python彩蛋_隐藏在Python中的15个彩蛋

    在python环境下输入这些命令,就会出现有趣的画面. 比如import this 彩蛋指电影中不仔细寻觅,会被忽略的有趣细节:还有就是影片剧情结束后,在演职员表滚屏时或之后出现的电影片段(通常是一些 ...

  3. Python之禅(The Zen of Python)源码分析

    简介

  4. Python能让你上天?带你挖掘隐藏彩蛋~(附代码)

    作者:Satwik Kansal 翻译:王婷 校对:丁楠雅 本文共1000字,建议阅读5分钟. 本文将带你挖掘Python中隐藏的彩蛋. Python当然能让你上天! 没试过?别担心,我来教你.和Py ...

  5. 用python写聊天机器人_用Python 写一个机器人陪你聊天(文尾有彩蛋)

    工作一忙,原来秉烛夜谈的好友现在都很少聊天,微信都成了微信群的天下,鲜有微信好友给你发消息,想要主动发却也找不到开题话题,怎么办?用Python写一个机器人陪自己聊聊天吧.以下是源码及解析,小白都看得 ...

  6. 必知的Python 五个精彩彩蛋

    在大家看来程序员都是沉默寡言很无趣的人,但是看看开发 Python 的元老们,时不时就在程序中加些彩蛋来娱乐普生大众,放松自我,现在就来细数一下 Python 中那些有趣的彩蛋 1.hello wor ...

  7. python zen_Python彩蛋--zen of python

    今天早上在公交上浏览博客的时候,发现了python里面的一个小彩蛋--zen of python 一首python之歌 我们来看一看... ​ 是不是很简单,在python shell 里 输入 im ...

  8. python彩蛋_盘点Python的5大彩蛋

    今天我才发现Python原来有5个彩蛋,今天就给大家盘点一下. 1:Hello world Hello world肯定是大家刚学习一门新的程序语言时输出的内容,Python输出Hello world大 ...

  9. Python程序员都知道的5个彩蛋

    程序员在大众眼中是一群智商高.情商低,少言寡语的人,这种由来已久的刻板印象源自于大众缺乏对程序员的真正了解,其实程序员和其它行业一样是个多元化的职业,也不乏幽默.高情商.口吐莲花的人. 看看开发 Py ...

最新文章

  1. 好好学python · 元组
  2. Mysql基础--常见的表的约束介绍(一)
  3. python实现验证码与进度条
  4. 新一代才子原创歌曲--《伤心日记》- 孔朝
  5. 如何在 SAP 电商云里设置 Time Restrictions
  6. [SHOI2002]百事世界杯之旅
  7. BOE(京东方)与吉利控股集团签订战略合作协议
  8. windows下openresty环境配置
  9. k8s学习:挂载 pvc
  10. CAD二次开发——cad查看对象C#:MdgDbg.dll
  11. 在计算机潮流计算的三类节点中,电力系统稳态分析复习(电管11) - 图文
  12. win7 正在配置 请勿关闭计算机,怎么办Win7开机显示正在配置Windows Update请勿关机...
  13. 厦门大学LaTeX模板:页眉页脚设置
  14. Wrong JPEG library version: library is 80, caller expects 62 解决办法
  15. 微信模板消息 群发 突破 4条限制
  16. 记录几个免费质量高的图库视频库
  17. QT5.15.2__wind10 64 源码编译
  18. Windows程序开机自启动
  19. 边工作边学习的现实选择
  20. 异形隔离java剧情_异形隔离剧情发展是升级必走的道路

热门文章

  1. 【RNN入门到实战】LSTM从入门到实战——实现空气质量预测
  2. iOS 浏览相册功能实现 —— HERO博客
  3. A Game of Thrones(33)
  4. Spring学习(上)
  5. Node之Express服务器启动安装与配置
  6. Solidworks安装失败,出现内部错误:该产品组件的windows installer没按预期运
  7. webuploader上传文件夹
  8. 计算机专业河南大学中外合办分数线,河南大学国际教育学院是中外合作办学吗 分数线多少...
  9. 教你获取Microsoft Office 365E5账号
  10. ksy是谁_sky为什么叫人皇:sky是谁及资料