I have an issue with the following code please.
(我对以下代码有疑问。)
Forgive me if not, I am a python
newbie.
(如果没有,请原谅我,我是python
新手。)
The following code is in my dodo.py
:
(以下代码在我的dodo.py
:)
def closed_over(par):
print("
??? " + par)
if par == "bar":
return False
else:
return True
def task_bug():
for par in ("foo", "bar"):
print("par: " + par)
# closure!
exist_fn = lambda: closed_over(par)
print(exist_fn)
yield {
"name": par,
"actions": [["echo", "action:", par]],
"verbosity": 2,
"uptodate": [exist_fn]
}
When I run doit bug:foo
I expect to NOT execute it as ( closed_over
returns True
), but:
(当我运行doit bug:foo
我希望不会以( closed_over
返回True
)的身份执行它,但是:)
par: foo
<function task_bug.<locals>.<lambda> at 0x7f8926f9a560>
par: bar
<function task_bug.<locals>.<lambda> at 0x7f8926f9a5f0>
??? bar <- par should be foo
. bug:foo
action: foo <- echo was called
As you can see above the two closures outside the yield
are different function objects but for some reason uptodate
is always calling the same.
(如您在上方看到的, yield
之外的两个闭包是不同的函数对象,但由于某种原因uptodate
总是调用相同的对象。)
ask by Andrea Richiardi translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…