I want to set up classes for the following graph structure:
- A graph is a list of graph_nodes
- A graph_node is an object with a property graph_data
- A graph_data is an object with a "value" property, which can either be a primitive or a graph.
This causes circular imports since the graph_data object needs to import the graph object. Is there a better way to structure this data structure? Obviously this is a very watered down example but I think this is the key problem with my code.
Here's an example of the circular import:
#file main.py:
from graph import graph
myGraph = graph()
#file graph.py
import graph_node
class graph:
graph_nodes = []
def __init__(self, graph_nodes=None):
if graph_nodes == None:
self.graph_nodes = graph_nodes
else:
for i in range(5):
self.graph_nodes.append(graph_node.graph_node())
#file graph_node.py
import graph_node_data
class graph_node:
graph_data = None
def __init__(self):
self.graph_data = graph_node_data.graph_node_data()
#file graph_node_data.py
import graph
class graph_node_data:
value = None
def __init__(self):
self.value = graph.graph(graph_nodes = None)
Here's a graph illustrating where this structure might be useful.

graph_nodeandgraph_node_datainto two classes/files ? looks like over-engineering to me.file graph.pyyou importgraph_nodebut you're not using it ;)