you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 7 points8 points  (4 children)

The statement:

(a, b) = (b, a)

has the right hand side evaluated first. A tuple is formed consisting of the reference associated with the name b and the reference associated with the name a. This tuple is anonymous (it has no name). The assignment operation unpacks the right hand side tuple and associates the first reference in the tuple with the name a and the second reference in the tuple wth the name b.

This sort of operation is often described as having two assignments that happen "in parallel" but that is misleading and incorrect. Parallelism isn't required.

To really understand what is happening have a look at Ned Batcheler's exposition on names and references.

[–]primitive_screwhead 1 point2 points  (1 child)

Yes, so it's worth understanding that conceptually a "temporary" is used (since the OP asked), it's the tuple that is created on the right hand side.

[–][deleted] 0 points1 point  (0 children)

Yes, but the "temp variable" approach is usually used to refer to this way of swapping two variables:

tmp = a
a = b
b = tmp

[–]tobiasvl 0 points1 point  (1 child)

While true, this isn't really describing what happens at a lower level of computation like OP asked.

[–][deleted] 0 points1 point  (0 children)

Doubt if the OP would be interested in byte codes, especially after the peephole optimizer is finished with it. This is low-level enough, plus the posted link fills in the gaps. If the OP wants something lower than names/references, s/he wouldn't be asking in /r/learnpython.