0

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.

1
  • Because your list doesn't only contain Node objects, it contains a type object, the type object Node, which you created the list with: self.queue = [Node]. Commented Nov 29, 2022 at 10:01

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

Comments

2

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 = []

    ...

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.