打字import this返回Tim Peters的Python之禅.但我注意到模块上有4个属性:
this.i
this.c
this.d
this.s
Run Code Online (Sandbox Code Playgroud)
我可以看到声明
print(''.join(this.d.get(el, el) for el in this.s))
Run Code Online (Sandbox Code Playgroud)
用于this.d解码this.s打印禅.
但有人可以告诉我属性this.i和属性this.c是什么?
我认为他们故意在那里 - 这个问题的答案似乎表明还有其他的笑话可以从禅宗的措辞中收集到.我想知道是否有一个参考我缺少这两个值.
我注意到Python版本之间的值不同:
# In v3.5:
this.c
Out[2]: 97
this.i
Out[3]: 25
# In v2.6
this.c
Out[8]: '!'
this.i
Out[9]: 25
Run Code Online (Sandbox Code Playgroud)
Mar*_*ers 36
i并且c只是循环变量,用于构建d字典.从模块源代码:
d = {}
for c in (65, 97):
for i in range(26):
d[chr(i+c)] = chr((i+13) % 26 + c)
Run Code Online (Sandbox Code Playgroud)
这构建了ROT-13映射 ; 每个ASCII字母(代码点65到90表示大写,97到122表示小写)映射到沿字母表的另一个ASCII字母13个点(循环回A和以后).所以A(ASCII点65)映射到N反之亦然(以及a映射到n):
>>> c, i = 65, 0
>>> chr(i + c)
'A'
>>> chr((i + 13) % 26 + c)
'N'
Run Code Online (Sandbox Code Playgroud)
请注意,如果您想自己编写ROT-13文本,则有一种更简单的方法; 只需使用rot13编解码器进行编码或解码:
>>> this.s
"Gur Mra bs Clguba, ol Gvz Crgref\n\nOrnhgvshy vf orggre guna htyl.\nRkcyvpvg vf orggre guna vzcyvpvg.\nFvzcyr vf orggre guna pbzcyrk.\nPbzcyrk vf orggre guna pbzcyvpngrq.\nSyng vf orggre guna arfgrq.\nFcnefr vf orggre guna qrafr.\nErnqnovyvgl pbhagf.\nFcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.\nNygubhtu cenpgvpnyvgl orngf chevgl.\nReebef fubhyq arire cnff fvyragyl.\nHayrff rkcyvpvgyl fvyraprq.\nVa gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.\nGurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.\nNygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.\nAbj vf orggre guna arire.\nNygubhtu arire vf bsgra orggre guna *evtug* abj.\nVs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.\nVs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.\nAnzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!"
>>> import codecs
>>> codecs.decode(this.s, 'rot13')
"The Zen of Python, by Tim Peters\n\nBeautiful is better than ugly.\nExplicit is better than implicit.\nSimple is better than complex.\nComplex is better than complicated.\nFlat is better than nested.\nSparse is better than dense.\nReadability counts.\nSpecial cases aren't special enough to break the rules.\nAlthough practicality beats purity.\nErrors should never pass silently.\nUnless explicitly silenced.\nIn the face of ambiguity, refuse the temptation to guess.\nThere should be one-- and preferably only one --obvious way to do it.\nAlthough that way may not be obvious at first unless you're Dutch.\nNow is better than never.\nAlthough never is often better than *right* now.\nIf the implementation is hard to explain, it's a bad idea.\nIf the implementation is easy to explain, it may be a good idea.\nNamespaces are one honking great idea -- let's do more of those!"
Run Code Online (Sandbox Code Playgroud)
至于Python 2.6(或Python 2.7)与Python 3.5的区别; 相同的变量名c也用于str.join()调用中的列表推导:
print "".join([d.get(c, c) for c in s])
Run Code Online (Sandbox Code Playgroud)
在Python 2中,列表推导没有自己的范围(与生成器表达式和dict和set comprehensions不同).在Python 3中他们这样做,并且c列表推导中的值不再是模块命名空间的一部分.因此,c 在模块范围中分配的最后一个值是97在Python 3中,并且this.s[-1]('!'在Python 2中是这样).请参阅为什么列表推导写入循环变量,但生成器不是?
这些1个字母的变量名称中没有嵌入笑话.还有是在禅本身的笑话.就像this模块的源代码和文本本身之间的事实一样,您可以找到违反所有规则的违规行为!