关于python:“this”模块的源代码是什么?

What is the source code of the “this” module doing?

如果打开一个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源代码(lib/this.py)中,此文本由一段奇怪的代码生成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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])

这称为ROT13编码:

1
2
3
4
d = {}
for c in (65, 97):
    for i in range(26):
        d[chr(i+c)] = chr((i+13) % 26 + c)

为大写字符(这是65的含义)和小写字符(这是97的含义)构建翻译表。

1
print"".join([d.get(c, c) for c in s])

打印翻译后的字符串。


如果你想用手或在头脑中进行ROT13替换,你可以检查这一点,因为13*2=26(英文字母的字母数),这本质上是一种交换:

1
2
3
4
5
6
7
8
9
10
11
a <-> n
b <-> o
c <-> p
...
m <-> z

A <-> N
B <-> O
C <-> P
...
M <-> Z

与LBH CENPGVFR YBAT RABHTU、LBH'YY RIRAGHNYL ZNFGRE GUR MRA BS EBG-13 NYTBEVGUZ NAQ ERNQ GUVF XYVATBA YBBXVAT GRKGF JVGUBHG PBZCHGRE URYC相比。


这是一个替代密码,ROT13。


这是一个替换密码(如前面的答案所述)。历史上讲,这是凯撒密码。

https://www.google.de/search?Q=凯撒+密码&cad=H


它使用ROT13编码。这是因为它是一个笑话。

也可以使用python函数对字符串进行解码。

Python 2只:

1
2
import this
print(this.s.decode('rot13'))

python 2和3:

1
2
import codecs
print(codecs.decode(this.s, 'rot-13'))