Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Attribute error

having problems with this error in python:

File "F:\dykrstra", line 36, in route
while node.label != node.prevNode.label:
AttributeError: 'NoneType' object has no attribute 'label'

Inside this while loop:

 while node.label != node.prevNode.label:
    node = node.prevNode
    labels.append(node.label)

I think it relates to this:

   def __init__(self, label):
        self.label = label
        self.neighbours = []
        self.distances = []
        self.prevNode = None
        self.totalDistance = 0

I'm not sure why prevNode doesn't like the nothing being assigned to it, please help.

like image 508
Ranger Avatar asked Nov 28 '25 03:11

Ranger


1 Answers

Your constructor sets self.prevNode to None, and later you try to access node.prevNode.label, which is like trying to access None.label. None doesn't have any attributes, so trying to access any will give you an AttributeError.

like image 136
Sven Marnach Avatar answered Nov 29 '25 17:11

Sven Marnach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!