0

I want to confidently use the Python idiom of swaping 2 values, a,b = b,a but I am not sure what is actually going on with this code. My guess is that, in memory, python creates a tuple containing b and a like (b,a) and then it changes a to point to the first element of the tuple and b to point to the second element.

user3463521
  • 572
  • 2
  • 7
  • 16
  • 3
    In terms of language semantics, yeah. In terms of implementation details, CPython optimizes it a bit, but that mostly doesn't matter. – user2357112 Apr 14 '19 at 22:23
  • 2
    You could look at `dis.dis('a,b = b,a')` – John Coleman Apr 14 '19 at 22:29
  • Sometimes closing a question as a duplicate seems like a criticism. In this case, I was at first planning to answer it myself but then realized that I couldn't write something nearly as informative as Martijn Pieters' answer of the other question. – John Coleman Apr 14 '19 at 22:44
  • That's the right thing to do, Thanks @JohnColeman! – user3463521 Apr 14 '19 at 22:47

0 Answers0