Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

doit - Python doit:子任务产生相同的最新可调用对象(Python doit: subtask yields same uptodate callable)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your problem is regarding python closure scoping (it scope variables not values)... Nothing to do with doit .

(您的问题与python闭包作用域有关(它的作用域变量不是值)...与doit无关。)

Probably have many questions about this here on Stack Overflow :)

(关于堆栈溢出,这里可能有很多问题:))

Python lambda closure scoping

(Python Lambda闭合作用域)

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总是调用相同的对象。)

No, it is not calling same closure.

(不,它没有调用相同的闭包。)

The problem is the same variable as parameter.

(问题是与参数相同的变量。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...