python - "this"模块的源代码是做什么的?

如果您打开 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)中,这段文本是由一段奇怪的代码生成的:

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编码:

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

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

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

打印翻译后的字符串。

关于python - "this"模块的源代码是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5855758/

相关文章:

linux - 查找 -exec cmd {} + vs | xargs

linux - 如何让 CRON 调用正确的路径

python - 如何搜索和替换文件中的文本?

c - Linux 中多线程的信号处理

python - 如何访问 Django 模板中的字典元素?

linux - 如何找出适合由 shell 脚本解析的 linux 机器的总物理内存 (RAM)?

android - 找不到构建工具修订版 23.0.1

linux - 每个虚拟主机的error_log?

python - 如何将 python 日期时间转换为具有可读格式日期的字符串?

python - Django 1.7 抛出 django.core.exceptions.AppR