简体   繁体   English

为什么该模块ROT13中的文本被编码?

[英]Why is the text in this module ROT13 encoded?

If we open the Python interpreter and type import this , as you may know, it will print the Zen of Python into the console. 如您所知,如果我们打开Python解释器并输入import this ,它将把Python的Zen打印到控制台中。 From the Python source code this text is generated by following piece of code. 该文本是从Python 源代码通过以下代码段生成的。

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])

I have read What is the source code of the "this" module doing? 我已经阅读了“ this”模块的源代码是什么? From the accepted answer I understood that the s variable text is ROT13 encoded and the last 6 lines 从接受的答案中,我了解到s可变文本是ROT13编码的,最后6行

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])

will decode by building translation table and printing out to the console. 将通过构建翻译表并打印到控制台进行解码。

Why it is encoded? 为什么要编码? In other words, why it is not written like this: 换句话说,为什么它不是这样写的:

s = """
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!"""

print(s)

Note: I searched similar question in SO but could not find helpful links. 注意:我在SO中搜索了类似的问题,但是找不到有用的链接。

This comment from @abarnert here says it all: @abarnert的评论在此说明了一切:

As a joke. 开个玩笑。 Everything the module does, from obfuscating the source code to implementing rot13 from scratch even though it's built into the stdlib, directly violates the Zen of Python. 该模块所做的一切,从混淆源代码到从头实现rot13(即使它已内置在stdlib中),都直接违反了Python的Zen。 Tim Peters also snuck some subtle jokes into the Zen itself (notice the dashes on the TOOWTDI line do it two different ways?) 蒂姆·彼得斯(Tim Peters)还对禅宗本身开了些微妙的笑话(请注意,TOOWTDI线上的破折号有两种不同的用法吗?)

Furthermore, efforts were made to remove the encoding , but it was rolled back in a subsequent PR . 此外,还努力删除了编码 ,但在随后的PR中将回滚

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM