Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluation time discrepancy 评估时间差异->执行时机差异 #3

Closed
liliao2017 opened this issue Dec 3, 2018 · 7 comments
Closed

Comments

@liliao2017
Copy link

这一小结主要讲的内容是生成器表达式中,in字句和条件语句会在不同的时间段被执行。原标题“评估时间差异”在没有看下文或者英文原文的情况下容易被理解为“评估时间长短上的区别”,可能会有一点费解。改为“执行时机差异”,会不会更容易理解?

@leisurelicht
Copy link
Owner

你好,感谢你提的建议。
我也觉得我这里的翻译歧义很大。
但是evaluation这个词本身没有执行的意思,直接改为"执行时机差异"感觉就跟原标题完全无关了。

@liliao2017
Copy link
Author

确实,evaluation本身不是计算机方面的专有名词,通常情况下都是理解为评估/评价。感觉原作者在这里的用词比较微妙,很难找到一个贴切的翻译。
不过结合下文来看,翻译为“执行”的好处就是可以让标题和下文内容保持一致。例如下面这句话“In a generator expression, the in clause is evaluated at declaration time, but the conditional clause is evaluated at runtime.”中的evaluate也翻译成了执行。

@leisurelicht
Copy link
Owner

你说的对,目前来看你的翻译更好。

我会进行修改并将你加入贡献者名单中。

@dbthinking
Copy link

dbthinking commented Mar 27, 2019

"在生成器表达式中, in 子句在声明时执行, 而条件子句则是在运行时执行."
读到这里时,我也有些困惑,查了原文是evaluated,感觉翻译为执行不太好,下意识以为是代码被执行了。
背后逻辑可能是将 “x in array” 语句中的x的未来循环变量指向了array的内存地址(确定相关变量在内存的位置),而条件子句则是在代码运行时定位变量在内存上的地址(新手不知理解的对不对)。所以导致了这一系列神奇的现象。

@leisurelicht
Copy link
Owner

@MaiYuYu 老实说,我没太看懂你想表达的意思。😂

所以说一下这里被翻译为执行的原因。

根据我的理解,下面这段代码

array = [1, 8, 15]
g = (x for x in array if array.count(x) > 0)
array = [2, 8, 22]

展开翻译后大概类似于这样

array = [1, 8, 15]
array1 = array[:]

def g():	
	for x in array:
		if array1.count(x):
			yield x 

array = [2, 8, 22]

print(list(g()))

for x in array 这一句的复制在申明的时候就已经执行了。而后面的判断语句的直到被调用的时候才执行。

@dbthinking
Copy link

@leisurelicht
我对Python学习还很浅,所以在理解与表述上有些错误。
嗯,代码是被执行了。但由于Python中yield的特性,真正计算x值是在其被使用时,所以才会有那个效果。
对yield概念的理解一直比较模糊,当时压根没往这方面思考 :p。

感谢你的解释。

@leisurelicht
Copy link
Owner

@MaiYuYu 因为小括号包起来的是生成器表达式而不是列表表达式。返回的是生成器对象,行为就跟yield出来的生成器函数是一样的了,你也可以使用next去调用这个对象。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants