"Maze Explorer" Project - function behaving like a class?

I am slightly confused by the setup of the project here:

So we defined two classes in separate scripts (Graph and Vertex) and then a function “build_graph”, which makes use of the classes.

It confuses me that we call methods from the class “Graph” on the variable excavation_site that is actually only a function. I am aware that the function by default assigns a variable graph = Graph() within.

But is it common practice to build a function that behaves like that?

I hope I made my confusion clear :stuck_out_tongue:

Below some of the code we use:

from graph import Graph, build_graph
from vertex import Vertex

excavation_site = build_graph()
excavation_site.explore()

def build_graph():
  graph = Graph()

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

  def explore(self):
    print("Exploring the graph....\n")
    current_room = "entrance"
    path_total = 0
    print ("\nStarting off at the {0}\n".format (current_room))

Are you calling methods on the function, or on what it returns? Does that function actually not return anything?

I don’t think wrapping instance creation inside a function is uncommon so long as it’s readable and consistent in style (for your own classes). Many built-ins create new objects all the time and it seems rather natural to do so.

1 Like

Ah yes, you are right. We are calling the method on what the function actually returns (which is graph, I didn’t paste that into the code)

Thanks a lot, that clears it up for me :slight_smile:

1 Like