0

For this while loop below. It is clear how the v1,v2,v3 are updated to new data objects based on the equation. But I do not understand how u1,u2,u3 retains the original values of v1,v2,v3.Can someone help clarify? So u1 points to the original data referenced by v1 but not the new reference for v1.

def findModInverse(a,m):
    if gcd(a,m) != 1:
        return None
    u1,u2,u3 = 1,0,a
    v1,v2,v3 = 0,1,m

    while v3 != 0:
        q = u3 // v3
        v1,v2,v3,u1,u2,u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3
    return u1 % m

    mod = findModInverse(7,26)

1 Answer 1

4

The right hand side of an assignment is evaluated first.

Then, if there are multiple targets, then the right-hand side should be a sequence and each element of that result is assigned from left-to-right to the targets.

This means that you first get a tuple with:

((u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3)

and then the results of that expression are assigned to the v1, v2, v3, u1, u2, u3 targets from left to right.

So first, the current values of v1, v2 and v3 are stored, and these 3 names are only rebound to their new values after all the right-hand-side expressions have been evaluated.

Also see How does swapping of members in the python tuples (a,b)=(b,a) work internally?

2
  • Thanks - This explains it - "So first, the current values of v1, v2 and v3 are stored, and these 3 names are only rebound to their new values after all the right-hand-side expressions have been evaluated"
    – scopa
    Commented Nov 20, 2014 at 17:57
  • Glad to have been of help! Feel free to accept my answer if you feel it was useful to you. :-) Commented Nov 20, 2014 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.