I want to create a Node class, and one of its attributes is a vector of Nodes (as in a linked list). Since Python does not declare the attribute type, I have no idea how to do it.
I want to create a Node class, and one of its attributes is a vector of Nodes (as in a linked list). Since Python does not declare the attribute type, I have no idea how to do it.
In a normal threaded list each node has an attribute that represents the next node only and not a list:
class Nodo:
def __init__(self, valor):
self.valor = valor
self.prox = None
If in your particular case you need to have an attribute that is a list for the various nodes you can do this:
class Nodo:
def __init__(self, valor):
self.valor = valor
self.nodos = [] #lista de Nodos
Now in this list you will add each Nodo
as you need to use, for example, the append
function:
self.nodos.append(Nodo(10))
In Python the type of the attribute is not declared, but the type is built according to its value. This means that if you want to have a list of elements of a certain type, you only need to add elements of that type to the list.