0

Getting caught up in a solution to NoneType errors stemming from using my functions add and append in the below code to an empty Double_list class object. Best way to avoid?

class Dbl_Node:

    def __init__(self, data):
        self.data = data
        self.next = None
        self.prev = None

class Double_list:

    def __init__(self): # Creates initial list w/ head and tail as None
        self.head = None
        self.tail = None

    def add(self, item): # adds node to beginning/head of list
        temp = self.head
        self.head = Dbl_Node(item)
        temp.prev = self.head
        self.head.next = temp

    def append(self, item): # adds node to end/tail of list
        temp = self.tail
        self.tail = Dbl_Node(item)
        self.tail.prev = temp
        temp.next = self.tail
3
  • 1
    You'll get better answers if you add the error messages to your question. Commented May 20, 2014 at 0:46
  • Sorry, for sure, here it is: 'in add temp.prev = self.head builtins.AttributeError: 'NoneType' object has no attribute 'prev' It is referring to the line with temp.prev = self.head Commented May 20, 2014 at 0:58
  • Thanks for the quick update. You can edit your question to add additional information by clicking the edit button under the question tags. This is the preferred method to add information to a question as things sometimes get lost in the comment threads. Commented May 20, 2014 at 1:01

2 Answers 2

1

You're initialising head and tail to None, but then trying to set prev and next members on them when inserting the first Dbl_Node.

def add(self, item):
    temp = self.head # on the first call to "add", self.head is None
                     # (as set in __init__) so temp is now None

    self.head = Dbl_Node(item) # create a new node and assign it to self.head
                               # this is fine

    temp.prev = self.head # this says assign the new node in self.head to the "prev"
                          # member of temp, however temp is None so the temp.prev part
                          # throws the error

You should check for this case

def add(self, item): # adds node to beginning/head of list
    temp = self.head
    self.head = Dbl_Node(item)
    if temp is not None:
        temp.prev = self.head

An alternate solution is to start with "dummy" nodes for the head and tail:

def __init__(self):
    self.head = Dbl_Node(None)
    self.tail = Dbl_Node(None)
    self.head.next = self.tail
    self.tail.prev = self.head

And then insert items in between these nodes

def add(self, item):
    temp = self.head.next
    self.head.next = Dbl_Node(item)
    temp.prev = self.head.next
    self.head.next.next = temp

Although I find this tends to needlessly complicate things a bit.

Sign up to request clarification or add additional context in comments.

3 Comments

My thought was that it would change from ` None ` to whatever was in self.head. Any good reason why it wouldn't just reassign temp.prev to the new variable instead of an error?
It's not that temp.prev is None, but temp itself. When you try to assign to temp.prev Python looks for a member called prev in the temp object - which is None, hence the exception. I'll annotate the above code with comments to illustrate it better.
Ohhh right right because temp is not referring to an entire node instance, but rather just self.head. I think I was mixing up temp as an entire node. Thanks for the explanation!
0
def append(self, item): # adds node to end/tail of list
        temp = self.tail
        self.tail = Dbl_Node(item)
        self.tail.prev = temp
        temp.next = self.tail

This will always raise a NoneType exception because self.tail is initially None then you assign a reference to it (None) to temp and try to assign something to None. Won't work.

YOu need to probably firstly assign a new object reference to self.tail

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.