输入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
i
和c
只是循环变量,用于构建d
字典。来自the module source code:这将构建一个ROT-13 mapping;每个ASCII字母(代码点65到90表示大写,97到122表示小写)映射到字母表中的另一个ASCII字母13位(循环回a并向前)。因此}):
^{pr2}$A
(ASCII点65)映射到N
,反之亦然(以及a
映射到{请注意,如果您想自己ROT-13文本,有一种更简单的方法;只需使用
rot13
编解码器进行编码或解码:至于Python2.6(或Python2.7)与Python3.5的区别,在
str.join()
调用的列表理解中也使用了相同的变量名c
:在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
模块的源代码和文本本身之间,您可以发现几乎所有规则的冲突!在相关问题 更多 >
编程相关推荐