I am new to Python and the following question is so difficult for me to understand.
a,b,c=1,2,3
a,b,c=c,a,b=b,a,c=c,b,a
print(a,b,c)
The output of this is -
(2,3,1)
but I don't understand why it isn't -
(3,2,1)
As norok indicated, storage and loading happen from different directions. If we take the following code we can see what python is doing under the hood.
import dis
def foo():
a, b, c = 1, 2, 3
a, b, c = c, a, b = b, a, c = c, b, a
dis.dis(foo)
Below you see the bytecode. To the right of the comments, you see the values for variables a
, b
, and c
as well as the memory stack at the end of the operation. You'll see the DUP_TOP
command negate the assignments at various steps so only the first load and last store appear to do anything. This would also explain why a, b, c = a, a, a = b, b, b = c, c, c = b, a, c = c, b, a
still evaluates to (2, 3, 1)
.
# a b c stack
4 0 LOAD_CONST 4 ((1, 2, 3)) # - - - [(1, 2, 3)]
3 UNPACK_SEQUENCE 3 # - - - [1, 2, 3]
6 STORE_FAST 0 (a) # 1 - - [2, 3]
9 STORE_FAST 1 (b) # 1 2 - [3]
12 STORE_FAST 2 (c) # 1 2 3 []
5 15 LOAD_FAST 2 (c) # 1 2 3 [3]
18 LOAD_FAST 1 (b) # 1 2 3 [3, 2]
21 LOAD_FAST 0 (a) # 1 2 3 [3, 2, 1]
24 BUILD_TUPLE 3 # 1 2 3 [(3, 2, 1)]
27 DUP_TOP # 1 2 3 [(3, 2, 1), (3, 2, 1)]
28 UNPACK_SEQUENCE 3 # 1 2 3 [3, 2, 1, (3, 2, 1)]
31 STORE_FAST 0 (a) # 3 2 3 [2, 1, (3, 2, 1)]
34 STORE_FAST 1 (b) # 3 2 3 [1, (3, 2, 1)]
37 STORE_FAST 2 (c) # 3 2 1 [(3, 2, 1)]
40 DUP_TOP # 3 2 1 [(3, 2, 1), (3, 2, 1)]
41 UNPACK_SEQUENCE 3 # 3 2 1 [3, 2, 1, (3, 2, 1)]
44 STORE_FAST 2 (c) # 3 2 3 [2, 1, (3, 2, 1)]
47 STORE_FAST 0 (a) # 2 2 3 [1, (3, 2, 1)]
50 STORE_FAST 1 (b) # 2 1 3 [(3, 2, 1)]
53 UNPACK_SEQUENCE 3 # 2 1 3 [3, 2, 1]
56 STORE_FAST 1 (b) # 2 3 3 [2, 1]
59 STORE_FAST 0 (a) # 2 3 3 [1]
62 STORE_FAST 2 (c) # 2 3 1 []