I have declared four variables [a=1,b=2,c=3,d=0] in python and swapping them in one line code using ‘,’ and ‘=’ (Simple Assignment Operator).
I have got multiple Answers and got confused. please help me…
Case 1:
a=1
b=2
c=3
d=0
a=a,b=b,c
print "a = " + str(a)
print "b = " + str(b)
print "c = " + str(c)
print "d = " + str(d)
Output of Case 1:
a = 2
b = 3
c = 3
d = 0
Case 2:
a=1
b=2
c=3
d=0
b=a,b=b,c
print "a = " + str(a)
print "b = " + str(b)
print "c = " + str(c)
print "d = " + str(d)
Output of Case 2:
a = 2
b = 3
c = 3
d = 0
Case 3:
a=1
b=2
c=3
d=0
c=a,b=b,c
print "a = " + str(a)
print "b = " + str(b)
print "c = " + str(c)
print "d = " + str(d)
Output of Case 3:
a = 2
b = 3
c = (2,3)
d = 0
Case 4:
a=1
b=2
c=3
d=0
d=a,b=b,c
print "a = " + str(a)
print "b = " + str(b)
print "c = " + str(c)
print "d = " + str(d)
Output of Case 4:
a = 2
b = 3
c = 3
d = (2,3)
Confusion is:
In the case number 3 and 4 the output is correct (as i expected). but in the case number 1 and 2 the value of a is 2 and value of b is 3. I expect the value should be (2,3). So what’s problem in my code?
[My Python Version is 2.7]
tl;dr: multiple assignments (multiple
=statements on one line) are evaluated from left-to-right, not from right-to-left (after evaluating the right-hand-side expression).To complicate matters, you are using tuple assignment and ‘normal’ assignment in a heady mix.
Tuple assignment uses one assignment operator, so to swap two variables, use:
The right-hand side must evaluate to a tuple of the same number of elements as there are variables on the left-hand side. You do that, so that’s fine.
Now, in your examples, you are not only unpacking tuples. When the left-hand side contains only one variable, the tuple is not unpacked, just simply assigned:
becomes
(2, 1).The fun starts when you use multiple assignments on the same line. These are processed from left to right.
So, the following simple example:
means that
abecomes1, thenb, thenc.Now we can understand each case:
a=a,b=b,c, wherea = 1,b = 2,c = 3.This becomes: evaluate
b, c->(2, 3), then assign that toa->a = (2, 3). Then assign it toa, b, soa = 2,b = 3. Result:a = 2,b = 3,c = 3.b=a,b=b,c, wherea = 1,b = 2,c = 3.Same as the case before, but now
b = (2, 3)is set first, thenb = 3again, same result as case 1.c=a,b=b,c, wherea = 1,b = 2,c = 3.Input on the right-hand side the same as cases 1. and 2., but now we first set
c = (2, 3). End result as expected,a = 2,b = 3,c = (2, 3).d=a,b=b,c, wherea = 1,b = 2,c = 3.Same as case 3. but now we set
dinstead. No surprises.What confused you here is that the assignments, after the right-hand side has been evaluated, are processed from left to right, not from right to left.
For cases like these, it’s actually easiest to run your code (wrapped in a function), through the
dis.dis()function to disassemble the python bytecode:This is the first case; note how after the
BUILD_TUPLEand theDUP_TOPopcode (the latter creates an extra copy on the stack to serve the extra assignment), the first thing that happens is theSTORE_FASToperation ona, followed by aUNPACK_SEQUENCE(the tuple assignment opcode), to then store the results intoaandb.