Home > Mobile >  Graph objects has no attribute nodes
Graph objects has no attribute nodes

Time:11-17

I know this is probably a very simple fix but can anyone spot why i'm being told type object 'Graph' has no attribute 'nodes' It is refering to the line "if name not in Graph.nodes"

def addrouter(name: AddRouter):
if name not in Graph.nodes:
    Graph.add_node(name)
    return "success"
else:
    return "Error, node already exists" 

This is my add node function

   def add_node(self, name):
   if name in self.nodes:
            return -1

    self.nodes.append(name)
    return 0

And here is the graph class

class Graph:
    def __init__(self):
    self.nodes = []
    self.edges = []

CodePudding user response:

Graph is a class. You need to create a new instance of the Graph class and operate on it.

my_graph = Graph()
if name not in my_graph.nodes:

You also don't necessarily need the if statement, as your add_node function already handles checking the list for the name.

  • Related