Python模块的属性`this`

2024-06-13 21:17:35 发布

您现在位置:Python中文网/ 问答频道 /正文

输入import this返回timpeters的Python Zen。但我注意到模块上有4个属性:

this.i 
this.c
this.d
this.s

我能看出那份声明

^{pr2}$

使用this.d解码this.s打印禅。在

但是有人能告诉我属性this.i和{}是用来做什么的吗?在

我假设他们是故意的-对this question的回答似乎暗示着还有其他笑话可以从禅宗的措辞中收集到。我想知道这两个值是否缺少一个引用。在

我注意到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

Tags: 模块inimport声明属性outthis解码
1条回答
网友
1楼 · 发布于 2024-06-13 21:17:35

ic只是循环变量,用于构建d字典。来自the module source code

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

这将构建一个ROT-13 mapping;每个ASCII字母(代码点65到90表示大写,97到122表示小写)映射到字母表中的另一个ASCII字母13位(循环回a并向前)。因此A(ASCII点65)映射到N,反之亦然(以及a映射到{}):

^{pr2}$

请注意,如果您想自己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!"

至于Python2.6(或Python2.7)与Python3.5的区别,在str.join()调用的列表理解中也使用了相同的变量名c

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

在python2中,列表理解没有自己的作用域(与生成器表达式和dict和set理解不同)。在python3中是这样的,并且列表理解中的c值不再是模块名称空间的一部分。因此,在模块作用域处分配给c的最后一个值在python3中是97,在python2中是this.s[-1](所以是'!')。见Why do list comprehensions write to the loop variable, but generators don't?

在这些1个字母的变量名中没有隐含的笑话。禅宗本身就有笑话。就像在this模块的源代码和文本本身之间,您可以发现几乎所有规则的冲突!在

相关问题 更多 >