Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

variable is redefined in augment assignment if it is used in closure

Here is the example:

def foo1():
    res = 0
    def foo2():
        nonlocal res
        res += 10
        return 20

    rv = foo2()
    res += rv

    print(res)

foo1()  # output: 30 

res is increased by 10 in closure, becoming 10, then plus 10 returned by foo2, resulting in 20 consequently. So far, so good.

Here is an another similar case:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def foo1():
    res = 0
    def foo2():
        nonlocal res
        res += 10
        return 20

    res += foo2()

    print(res)

foo1()  # output: 20

Strangely, it outputs 10.

It seems that res is re-defined in res += foo2(). Do I think right?

Why is the result 10 at above example? what results in this?

  • CPython 3.6.9

>Solution :

Based on the symptom, in stack pseudo-code it is acting like

push res, push foo2(), add, pop res

So the change to res in the middle of foo2 has no effect on the result. Because it "grabbed" the value of res BEFORE foo2, and used that in the expression.

Or you could show the pseudo-code like this:

temp1 = res
temp2 = foo2()
temp1 = temp1 + temp2
res = temp1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading