Graph question

I am having a hard time understanding some codes. In class Graph function add_edge, there are lines

self.graph_dict[from_vertex.value].add_edge(to_vertex.value)
    if not self.directed:
      self.graph_dict[to_vertex.value].add_edge(from_vertex.value)

I don’t quite understand the codes, can someone help me understand? I guess the particular part that I don’t understand is “.add_edge(to_vertex.value)” part. It is a method of vertex object but why appear here? is “self.graph_dict[from_vertex.value]” an vertex object? If so, it is in a different file, how is it usable here?

class Graph:
  def __init__(self, directed = False):
    self.graph_dict = {}
    self.directed = directed

  def add_vertex(self, vertex):
    self.graph_dict[vertex.value] = vertex
  
  def add_edge(self, from_vertex, to_vertex):
    print("Adding edge from {0} to {1}".format(from_vertex.value, to_vertex.value))
    self.graph_dict[from_vertex.value].add_edge(to_vertex.value)
    if not self.directed:
      self.graph_dict[to_vertex.value].add_edge(from_vertex.value)

class Graph:
  def __init__(self, directed = False):
    self.graph_dict = {}
    self.directed = directed

  def add_vertex(self, vertex):
    self.graph_dict[vertex.value] = vertex
  
  def add_edge(self, from_vertex, to_vertex):
    print("Adding edge from {0} to {1}".format(from_vertex.value, to_vertex.value))
    self.graph_dict[from_vertex.value].add_edge(to_vertex.value)
    if not self.directed:
      self.graph_dict[to_vertex.value].add_edge(from_vertex.value)

Please try to include a link to any lesson you have a query on. It does help. I’m assuming the Vertex class was already imported at the start of this script?

If you have an instance of the vertex class and you’d already added it to the graph_dict attribute of a Graph instance (such as Graph().add_vertex(vertex_instance)) then that dictionary look-up would make more sense. Without populating that dictionary it’d just be a key error.

https://www.codecademy.com/paths/computer-science/tracks/complex-data-structures/modules/cspath-graphs/lessons/python-graphs/exercises/python-graphs-graph-ii
Above is the URL.
To my surprise, there is no import of Vertex class. That is what confuses me.
You can look up the solution yourself once you go to the URL above. Click get help bottom right corner and get solution. It is the third from the top.

I’m afraid I can’t run the lesson without doing the previous lessons which I don’t have time for at the minute. But I can see there are three open tabs. One is named script.py and I’m assuming that’s what actually runs and imports from both graph and vertex modules.

@tgrtim is right.

Don’t forget that parameters (like from_vertex and to_vertex) act as placeholders until they get there value from the argument(s) provided at method call

the methods are called in main module (main.py), where you do import Graph and Vertex.

Yes. there are Graph.py, Vertex.py, and script.py. And script.py is whay actually runs and imports the other two files. I guess then it does not matter then whether Graph.py imports or not since it is not the file that runs.