-5

Could anyone tell me the difference between the normal assignment and the comma separator assignment?

Normal Assignment:

prev.next = head.next
head.next = None
head = head.next

Comma Separator Assignment:

prev.next,head.next,head= head.next,None,head.next

How the order of execution will vary in both cases? Could anyone explain the evaluation order?

Thanks in Advance.

DRV
  • 510
  • 6
  • 17
  • I believe Python creates temporary variables to capture the tuple on the right side first, then performs the assignment. There's an idiom in python where you can switch the values of two variables in one line of code: `a, b = b, a` – Ederic Oytas Jan 22 '22 at 05:38

2 Answers2

1

The right hand side of the equals sign is always evaluated before assigning to values on the left hand side regardless of if you are unpacking or not.

Kraigolas
  • 3,728
  • 3
  • 8
  • 27
0

Writing this:

a,b,c = 1,2,3

is like writing this

temp = (1, 2, 3)
(a, b, c) = temp

The order is fixed:

An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right.

TessellatingHeckler
  • 24,312
  • 4
  • 40
  • 77