简体   繁体   中英

Python AttributeError: type object has no attribute

I have a simple node class with Id and Value, but python seems to not be able to access those attributes when i use the objects in a list. This is the class Node for context.

class Node():
    def __init__(self, id : int, value : int):
        self.id = id
        self.value = value

This is a priority queue implementation (or at least a try to do so) where the error comes from

class ListAlt():
    def __init__(self):
        self.queue = [Node]
    def append(self, node : Node):
        self.queue.append(node)
    def dequeue(self):
        idMax = 0
        i = 0
        for nodo in self.queue:
            if (nodo.value > self.queue[idMax].value):
                idMax = i
            i += 1
        result = self.queue[idMax]
        return result

And this is the full code

#!/usr/bin/python3

class Node():
    def __init__(self, id : int, value : int):
        self.id = id
        self.value = value
class ListAlt():
    def __init__(self):
        self.queue = [Node]
    def append(self, node : Node):
        self.queue.append(node)
    def dequeue(self):
        idMax = 0
        i = 0
        for nodo in self.queue:
            if (nodo.value > self.queue[idMax].value):
                idMax = i
            i += 1
        result = self.queue[idMax]
        return result


n1 = Node(1,10)
n2 = Node(2,3)
n3 = Node(3,6)

lista = ListAlt()
lista.append(n1)
lista.append(n2)
lista.append(n3)

print(lista.dequeue())

Now, i can access the values of n1,n2,n3 directly, but inside the ListAlt object in this exact line

if (nodo.value > self.queue[idMax].value):

it throws the exception saying "AttributeError: type object 'Node' has no attribute 'value'" it should have printed "10" if everything worked correctly.

This is the problem:

class ListAlt():
    def __init__(self):
        self.queue = [Node]  # <--

You are setting queue to a list containing the Node class . Why not just an empty list?

Also, dequeue returns a Node instance. So to get 10 , you need to write this instead:

print(lista.dequeue().value)

Here is how I would change that code:

class Node:
    def __init__(self, node_id: int, value: int) -> None:
        self.id = node_id
        self.value = value

class ListAlt:
    def __init__(self) -> None:
        self.queue: list[Node] = []

    def append(self, node: Node) -> None:
        self.queue.append(node)

    def dequeue(self) -> Node:
        id_max = 0
        i = 0
        for nodo in self.queue:
            if nodo.value > self.queue[id_max].value:
                id_max = i
            i += 1
        result = self.queue[id_max]
        return result

if __name__ == "__main__":
    n1 = Node(1, 10)
    n2 = Node(2, 3)
    n3 = Node(3, 6)
    
    lista = ListAlt()
    lista.append(n1)
    lista.append(n2)
    lista.append(n3)
    
    print(lista.dequeue().value)

You are initializing your queue with the type Node . So, it is trying to get the value attribute of the type, which does not exist. Change your code with:

class ListAlt():
    def __init__(self):
        self.queue = []

    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM