Python attribute error

has 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 this applies 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 that nothing is assigned to it, please help.

+3
source share
3 answers

Your constructor sets self.prevNodeto None, and then you try to access node.prevNode.label, which is like trying to access None.label. Nonehas no attributes, so trying to access any of them will give you AttributeError.

+6
source

( ) None.label. , node None, , .

while node.label != node.prevNode.label:
    node = node.prevNode
    if node is not None:
        labels.append(node.label)
0

An AttributeError exception occurs when an object attribute is not available. the attribute reference is primary, followed by a period and a name.

So basically you need to double check your object and attribute name.

For example, to return a list of valid attributes for this object, use dir():

dir(node)
0
source

All Articles