1

Is assigning

a,b = c,a

the same as

b,a = a,c

It seems to be the same for the simple case where a, b, and c are numbers.

Is there a case where this fails?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Kevin
  • 977
  • 1
  • 10
  • 17
  • It's the same no matter what – iScrE4m Sep 03 '16 at 18:07
  • You completely changed the question. You now included *attribute assignment on the same object*. Assignment takes place from left to right, and you have **two `a` references** in your left-hand-side (one with an attribute). – Martijn Pieters Sep 03 '16 at 18:25
  • I've rolled back your edit, because you replaced `a, b = c, a` with `a, a.b = c, a`, which is *not the same thing by far*. – Martijn Pieters Sep 03 '16 at 18:26
  • So a, a.b =c, a is different from a.b, a = a,c. Should I make a new question about this or could you explain it here? Thanks. – Kevin Sep 03 '16 at 18:29
  • 1
    @Renegade: I've addressed the issue you are facing already; the important thing to remember here is that the right-hand side is evaluated *first*, and assignment takes place second, from left to right. – Martijn Pieters Sep 03 '16 at 18:29
  • 1
    @Renegade: your 'real problem' would be a duplicate of [Python Assignment Operator Precedence - (a, b) = a\[b\] = {}, 5](https://stackoverflow.com/q/32127908), really. – Martijn Pieters Sep 03 '16 at 18:30

3 Answers3

5

No, there is no case where this fails. The two statements are exactly equivalent, because a and b are independent.

That's because the right-hand side values are put on the stack first before assignment takes place (from left to right). Assigning to a before b makes no difference, because a and b are entirely different names, and assignment to either one first won't affect the other.

So the first statement:

a,b = c,a

is executed as (psuedo-bytecode*)

PUSH C ON STACK
PUSH A ON STACK
SWAP TOP TWO VALUES OF STACK
POP TOP OF STACK, STORE IN A
POP TOP OF STACK, STORE IN B

while the second

b,a = a,c

is executed as

PUSH A ON STACK
PUSH C ON STACK
SWAP TOP TWO VALUES OF STACK
POP TOP OF STACK, STORE IN B
POP TOP OF STACK, STORE IN A

It doesn't matter what type the values referenced by a, b or c are.

Things can get more confusing if you were to chain assignments and / or mix in subscriptions. In all those cases the names being assigned to are not independent, and people usually don't realise that all assignment takes place after the right-hand side expression has been executed and that assignment takes place from left to right after that.


* See How does swapping of members in the python tuples (a,b)=(b,a) work internally? for actual bytecode examples.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • @Renegade: read my answer carefully: *the right-hand side values are put on the stack first before assignment takes place (from left to right).* So when you assign to `current, current.node` from left to right, what do you think `current.node` is by the time the first assignment to `current` has been executed? – Martijn Pieters Sep 03 '16 at 18:27
1

It's the same in all cases, no matter what you're working with. So long as statements are equivalent (like yours are), the result will be the same.

arnbobo
  • 2,515
  • 2
  • 12
  • 18
0

Short Answer ----- "Yes".. a,b = c,a means a=c and b=a ... b,a = a,c means b=a and a=c

SumanKalyan
  • 1,681
  • 14
  • 24