Python: A, b = 1,2 and a = 1; b = 2 are strictly equivalent?

I am puzzled by the following:

It works:

a, b = 1071, 1029
while(a%b != 0):
    a, b = b, a%b

      

But the following snippet returns a ZeroDivisionError :

a, b = 1071, 1029
while(a%b != 0):
    a = b; b = a%b

      

while I expected both to be strictly equivalent.

Can anyone tell us about this please?

+3


source to share


1 answer


Not. IN

a, b = b, a%b

      

the right-hand side is first evaluated into a tuple, so it is a%b

evaluated using the original value a

. On the contrary,



a = b; b = a%b

      

a%b

computed after a

, assigned a value b

, assigning a different result b

.

+10


source







All Articles