Python模块`this`的属性

时间:2016-05-18 13:35:19

标签: python

键入<LinearLayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:weightSum="1" > <EditText android:id="@+id/et" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".8" android:text="aaa" android:background="@drawable/edit_border" /> <Button android:id="@+id/bt" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".2" android:text="Send" android:background="@drawable/button_border" /> </LinearLayout> 会返回Tim Peters的Python之禅。但我注意到模块上有4个属性:

import this

我可以看到声明

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

使用print(''.join(this.d.get(el, el) for el in this.s)) 解码this.d来打印Zen。

但有人可以告诉我this.sthis.i的属性是什么吗?

我认为他们故意在那里 - this question的答案似乎表明还有其他笑话可以从禅宗的措辞中收集到。我想知道是否有这两个值的参考文献。

我注意到Python版本之间的值不同:

this.c

1 个答案:

答案 0 :(得分:33)

connect(emiterObject, SIGNAL(someSignal(A)), targetObject, SLOT(clickButton(A))); i只是循环变量,用于构建c字典。来自the module source code

d

这构建了一个ROT-13 mapping;每个ASCII字母(代码点65到90表示大写,97到122表示小写)映射到沿字母表的另一个ASCII字母13个点(循环回A和以后)。因此d = {} for c in (65, 97): for i in range(26): d[chr(i+c)] = chr((i+13) % 26 + c) (ASCII点65)映射到A,反之亦然(以及映射到N的{​​{1}}):

a

请注意,如果您想自己编写ROT-13文本,则有一种更简单的方法;只需使用n编解码器进行编码或解码:

>>> c, i = 65, 0
>>> chr(i + c)
'A'
>>> chr((i + 13) % 26 + c)
'N'

至于Python 2.6(或Python 2.7)与Python 3.5的区别; 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!"

c

在Python 2中,列表推导没有自己的范围(与生成器表达式和字典和集合理解不同)。在Python 3中他们这样做,并且列表推导中的str.join()值不再是模块命名空间的一部分。因此,在模块范围中分配给print "".join([d.get(c, c) for c in s]) 的最后一个值在Python 3中为c,在Python 2中为c(因此为97)见Why do list comprehensions write to the loop variable, but generators don't?

这些1个字母的变量名称中没有嵌入笑话。禅本身里有的笑话。就像this.s[-1]模块的源代码和文本本身之间的事实一样,您可以找到违反所有规则的违规行为!