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:
a, b = b, a
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:
a, b = 1, 2
a = b, a
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:
a = b = c = 1
means that a becomes 1, then b, then c.
Now we can understand each case:
a=a,b=b,c, where a = 1, b = 2, c = 3.
This becomes: evaluate b, c -> (2, 3), then assign that to a -> a = (2, 3). Then assign it to a, b, so a = 2, b = 3. Result: a = 2, b = 3, c = 3.
b=a,b=b,c, where a = 1, b = 2, c = 3.
Same as the case before, but now b = (2, 3) is set first, then b = 3 again, same result as case 1.
c=a,b=b,c, where a = 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, where a = 1, b = 2, c = 3.
Same as case 3. but now we set d instead. 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:
>>> import dis
>>> def f(): a=a,b=b,c
...
>>> dis.dis(f)
1 0 LOAD_FAST 0 (b)
3 LOAD_GLOBAL 0 (c)
6 BUILD_TUPLE 2
9 DUP_TOP
10 STORE_FAST 1 (a)
13 UNPACK_SEQUENCE 2
16 STORE_FAST 1 (a)
19 STORE_FAST 0 (b)
22 LOAD_CONST 0 (None)
25 RETURN_VALUE
This is the first case; note how after the BUILD_TUPLE and the DUP_TOP opcode (the latter creates an extra copy on the stack to serve the extra assignment), the first thing that happens is the STORE_FAST operation on a, followed by a UNPACK_SEQUENCE (the tuple assignment opcode), to then store the results into a and b.