Reassigning multiple variables in one line

Daniel Q

I'm trying to understand some code that reverses a linked list.

Here's how I'm constructing a linked list:

class Node:
     def __init__(self, value, next=None):
          self.value = value
          self.next = next

def initialize():
     start = Node(0)
     prev = start
     for i in range(1, 6):
          cur = Node(i)
          prev.next = cur
          prev = cur
     return start

Here's the correct code in the answer:

def reverse_list(head):
     new_head = None
     while head:
          head.next, head, new_head = new_head, head.next, head 
     return new_head

And here's what I have:

def reverse_list(head):
     new_head = None
     while head:
          new_head, head, head.next = head, head.next, new_head 
     return new_head

However, my code throws an AttributeError when reversing a linked list:

AttributeError: 'NoneType' object has no attribute 'next'

I'm using the exactly same replacements as the correct answer, only in a different order. Why does my code throw an error? Does order matter when it comes to one line variable reassignments?

Dani Mesejo

The problem is that head.next becomes None after the first iteration. Then on the second iteration head gets assigned to head.next i.e. is set to None before calling head.next (on the left side) hence the error. For instance let's have the following scenario:

class Spam:

def __init__(self):
    self.foo = 1
    self.bar = 2


spam = Spam()
spam.foo, spam.bar = spam.bar, spam.foo
print(spam.foo, spam.bar) # 2, 1 as expected

but when we do:

spam.foo, spam, spam.bar = spam.bar, None, spam.foo

results in:

Traceback (most recent call last): File "main.py", line 9, in spam.foo, spam, spam.bar = spam.bar, None, spam.foo AttributeError: 'NoneType' object has no attribute 'bar'

So to answer your question the order does matter. In fact changing the order of the toy example to:

spam.foo, spam.bar, spam = spam.bar, spam.foo, None

does not throw any exceptions.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

print multiple variables and a string literal on one line?

Dart declare multiple variables on one line

Declare multiple variables on one line in perl

Can I use += on multiple variables on one line?

read multiple variables with scanner in one line

Assigning positional parameters to multiple variables in one line

How to output multiple variables in one line

Setting Multiple Variables to the Same Value in One Line

Defining multiple variables at once in one line with one value?

Reassigning Variables via Destructuring

Reassigning Variables in Tensorflow and scope

Would like to assign multiple variables from split() on one line of code

how to declare multiple variables in one line in oracle plsql block

Dart multiple function calls/set variables all on one line

How to declare multiple register variables in one line in c?

How Python assign multiple variables at one line works?

Assign a single value to multiple variables in one line in Rust?

can I use multiple variables in one j2 line?

Can I Declare and initialize multiple variables in one line with a tuple?

How to reference multiple selector variables in one line with jQuery

How to initialize multiple variables of some type in one line

How to assign same value to multiple variables in one line?

Iterate over an array and initialize multiple variables in one line in Ruby

How to Set multiple Variables value in one Line in C#?

How to replace multiple variables in one line using Stata

Having trouble with reading a txt file with one line and multiple variables

Declare multiple variables in one line in Spock when: block

How do I declare multiple array variables on one line - python

Delcare multiple variables on one line using os.getenv in python?