When one uses a multiple assignment in Python, such as
a, b, c = b, c, a
I've always thought that the relative order of the arguments is irrelevant (as long as one is consistent on both sides), i.e., the result would be the same if one does
c, a, b = a, b, c
While this appears to be true when a, b, c
are independent variables, it seems that it may not be true when they are related.
For instance, if we define a node of a linked list as follows:
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
and write a function to reverse a linked list in-place:
def reverseList(head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head is None:
return head
slow = None
fast = head
while fast is not None:
fast.next, slow, fast = slow, fast, fast.next
return slow
then this works fine, for instance:
head = ListNode(1)
head.next = ListNode(2)
reverseList(head)
However, if we replace the line in the loop with a different assignment ordering:
fast, fast.next, slow = fast.next, slow, fast
then we get an error
AttributeError: 'NoneType' object has no attribute 'next'
In the second (last) iteration, both fast
and slow
are not None
and fast.next
is None
, but I still don't understand why this causes an error? Shouldn't the multiple assignments be done in parallel?
More generally, when and when not could one change the order of a multiple assignment statement without affecting the result?
Edit: I've read this question, and while it is related I still don't get how the answers there help to explain my issue. I would appreciate any elaboration on my case.