tel*_*tel 8 python obfuscation execution control-flow
我正在阅读关于Stack Overflow(Python的禅宗)的另一个问题,我在Jaime Soriano的答案中遇到了这一行:
import this
"".join([c in this.d and this.d[c] or c for c in this.s])
Run Code Online (Sandbox Code Playgroud)
在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!"
Run Code Online (Sandbox Code Playgroud)
所以我当然不得不花费整个上午试图理解上面的列表...理解......事情.我毫不犹豫地断然宣布它是混淆的,但这只是因为我已经编程了一个半月,因此我不确定这些结构在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!"
Run Code Online (Sandbox Code Playgroud)
并this.d包含一个字典与解码的cypher 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'}
Run Code Online (Sandbox Code Playgroud)
据我所知,Jaime代码中的执行流程如下:
1.循环c for c in this.s为c赋值
.如果语句的c in this.d计算结果为True,则"and"语句执行其直接执行的任何操作在这种情况下this.d[c].
3.如果语句的c in this.d计算结果为False(Jaime的代码中永远不会发生),则"或"语句会执行其直接发生的任何事情,在本例中为循环c for c in this.s.
我对这个流程是否正确?
即使我对执行的顺序是正确的,这仍然给我留下了大量的问题.为什么<1>是第一个执行的东西,即使它的代码在几个条件语句之后排在最后?换句话说,为什么for循环开始执行并赋值,但是实际上只在代码执行的后一点实际返回一个值,如果有的话?
另外,对于奖励积分,Zen文件中有关荷兰语的奇怪线条是什么?
编辑:虽然现在说它让我感到羞耻,但直到三秒钟之前我才认为Guido van Rossum是意大利人.在阅读他的维基百科文章之后,我至少会抓住,如果不完全理解,为什么那条线在那里.
sth*_*sth 11
列表理解行中的运算符如下关联:
"".join([(((c in this.d) and this.d[c]) or c) for c in this.s])
Run Code Online (Sandbox Code Playgroud)
删除列表理解:
result = []
for c in this.s:
result.append(((c in this.d) and this.d[c]) or c)
print "".join(result)
Run Code Online (Sandbox Code Playgroud)
删除and/ orboolean trickery,用于模拟if- else语句:
result = []
for c in this.s:
if c in this.d:
result.append(this.d[c])
else:
result.append(c)
print "".join(result)
Run Code Online (Sandbox Code Playgroud)