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.
Nodeobjects, it contains atypeobject, thetypeobjectNode, which you created the list with:self.queue = [Node].