我正在阅读 Stack Overflow 上的另一个问题(Python 之禅),在 Jaime Soriano 的回答中,我遇到了这一行:
import this
"".join([c in this.d and this.d[c] or c for c in this.s])
在Python shell中输入以上内容会打印出:
"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!"
当然,我不得不花费整个上午的时间来理解上面的列表……理解……事情。我不愿意断言它是混淆的,只是因为我只编程了一个半月,所以不确定这样的结构在Python中是否常见。
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!"
而this.d
则包含了解密this.s
所需的密码字典:
{'A': 'N', 'C': 'P', 'B': 'O', 'E': 'R', 'D': 'Q', 'G': 'T', 'F': 'S', 'I': 'V', 'H': 'U', 'K': 'X', 'J': 'W', 'M': 'Z', 'L': 'Y', 'O': 'B', 'N': 'A', 'Q': 'D', 'P': 'C', 'S': 'F', 'R': 'E', 'U': 'H', 'T': 'G', 'W': 'J', 'V': 'I', 'Y': 'L', 'X': 'K', 'Z': 'M', 'a': 'n', 'c': 'p', 'b': 'o', 'e': 'r', 'd': 'q', 'g': 't', 'f': 's', 'i': 'v', 'h': 'u', 'k': 'x', 'j': 'w', 'm': 'z', 'l': 'y', 'o': 'b', 'n': 'a', 'q': 'd', 'p': 'c', 's': 'f', 'r': 'e', 'u': 'h', 't': 'g', 'w': 'j', 'v': 'i', 'y': 'l', 'x': 'k', 'z': 'm'}
据我观察,Jaime的代码执行流程如下:
1.循环
c for c in this.s
给c赋值2.如果语句
c in this.d
为True,则“and”语句执行其右侧的任何内容,在本例中为this.d[c]
。3.如果语句
c in this.d
为False(在Jaime的代码中从未发生过),则“or”语句执行其右侧的任何内容,在本例中为循环c for c in this.s
。我对执行顺序正确吗?
即使我关于执行顺序是正确的,这仍然让我有很多问题。为什么<1>是要执行的第一件事,即使它的代码出现在几个条件语句之后?换句话说,为什么
for
循环开始执行并分配值,但只在代码执行的后期(如果有的话)才实际返回值?此外,作为额外的奖励问题,关于Zen文件中荷兰人的奇怪行是什么意思? 编辑:虽然现在说出来让我感到耻辱,但三秒钟前我还以为Guido van Rossum是意大利人。阅读了他的维基百科文章后,我至少明白了为什么Zen文件中有那一行。
this
- 一个很好的指导方针,可以作为一个“不要这样做”的例子。 ;) - user395760