I'm trying to create several class instances of graphs and initialize each one with an empty set, so that I can add in-neighbors/out-neighbors to each instance:
class Graphs:
def __init__(self, name, in_neighbors=None, out_neighbors=None):
self.name = name
if in_neighbors is None:
self.in_neighbors = set()
if out_neighbors is None:
self.out_neighbors = set()
def add_in_neighbors(self, neighbor):
in_neighbors.add(neighbor)
def add_out_neighbors(self, neighbor):
out_neighbors.add(neighbor)
def print_in_neighbors(self):
print(list(in_neighbors))
graph_names = [1,2,3,33]
graph_instances = {}
for graph in graph_names:
graph_instances[graph] = Graphs(graph)
However, when I try to add an in-neighbor:
graph_instances[1].add_in_neighbors('1')
I get the following error:
NameError: name 'in_neighbors' is not defined
I was following this SO question that has a class instance initialized with a list
, but I couldn't figure out where I'm mistaken