3

While watching a video explaining about a quiz I found this code snippet:

a, x = x, a
a, x = x, a
print a
print x

The video says that the end result is x and a swap, and if we do that again, it's going to be in the original place which doesn't change the value of any variables.

My question is, if I first time assign a => x and x => a, then the value of code is not going to be changed even if we do the exact same code and assign the same value for the same variable as I did just before. But why does the video explain that the value of the a and x are going to be swapped twice and going to get the same value before executing the statement?

Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
장은수
  • 61
  • 1
  • 6
  • For the first question, as soon as you do `x = a`, you've lost the value of `x` if it wasn't assigned somewhere else, so they couldn't swap. You would need a third variable (usually called something like `temp`) to hold the value of `x` to do a swap; unless I'm missing some kind of fringe technique to achieve this. – Carcigenicate Nov 03 '16 at 15:50
  • 3
    `a = x, x = a` is an error. Did you mean `a,x = x,a`? – John Gordon Nov 03 '16 at 15:52
  • 1
    @Carcigenicate you could easily just use a,x = x,a to not need a temp variable. – Whud Nov 03 '16 at 15:53
  • @JohnGordon Sorry. I meant a, x = x, a. I'll edit it. – 장은수 Nov 03 '16 at 15:55
  • @Whud Some third variable would implicitly have to be created wouldn't it? I've never done this in Python before, but again, once you overwrite `a`, the value is gone. If Python's making a copy in the background, then I guess that would work. – Carcigenicate Nov 03 '16 at 15:56
  • @Carcigenicate No you would not need to have a third value if you used it in on statement because the new value of 'a' and 'x' are not set until the statement is complete . – Whud Nov 03 '16 at 15:58
  • @Carcigenicate sorry, I found a better [question/answer](http://stackoverflow.com/questions/21047524/how-does-swapping-of-members-in-the-python-tuples-a-b-b-a-work-internally) to explain. – roganjosh Nov 03 '16 at 16:00
  • I'm not sure what the question here is. If you swap twice then the objects will be bound to their original names. If you only swap once then the bindings will be swapped. – Ignacio Vazquez-Abrams Nov 03 '16 at 16:03
  • @IgnacioVazquez-Abrams (According to the video,) I mean, executing `a, x = x, a` once is assigning value x to the variable a and assigning value a to the variable x. But if I execute the same code twice, then a is going to be x but a is already x, and x is going to be a but x is already a. So if I execute the code twice, I expected the value that is opposite from before executing the statement, but if I really run it in the python interpreter, it isn't. The value wasn't changed for both of the variables. That is what I'm curious about. – 장은수 Nov 03 '16 at 16:09
  • Ah, I see where the confusion is now. – Ignacio Vazquez-Abrams Nov 03 '16 at 16:13
  • @장은수 let's assume `a = 2` and `x = 3` at the start. `a, x = x, a` so value `a` --> `x` and value `x` --> `a`. Now, `a =3` and `x = 2`. Then we repeat the operation. Value `a` (which is now 3) goes back to `x` and value `x` (which is 2) goes back to `a`. Why would it not have the original value if you effectively just reversed the first operation? – roganjosh Nov 03 '16 at 16:13

1 Answers1

4

The syntax a,x = x,a swaps the values because the assignments to the right hand side of the assignment operator (=) are first evaluated and then assigned to their values, as explained here. It logically follows that if this operation is performed twice, then the second swap returns the variables to their original values.

Note that if instead you wrote a = x; x = a then a and x would both end up as the start value of x because each statement is evaluated sequentially from left to right.

Demonstration:

>>> a = 1
>>> x = 2
>>> a,x = x,a
>>> a,x
(2, 1)
>>> 
>>> a = 1
>>> x = 2
>>> a = x; x = a
>>> a,x
(2, 2)
Community
  • 1
  • 1
Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
  • 1
    Syntactically speaking, the right side of ``a,x = x,a`` creates a tuple with two elements, and then the left side unpacks the tuple into two variables. However, starting with Python 2.6, an internal optimization recognizes this common idiom, and skips the creation of the tuple. – jasonharper Nov 03 '16 at 16:12