Several posts I have read have not given a clear answer, so if there is something I missed, feel free to point me in the right direction.
In my code, I am passing mutable collections objects as arguments into my function call. The code outputs the results I expect. However, whenever I use test against multiple cases, i.e. foo(1) foo(2) foo(3) foo(4),
Then whatever result returns from foo(1) will get passed into foo(2), into foo(3), etc. So if I comment out each test case individually, I am fine. If I try to run them in one main call, then every call returns to each new call. I have tried using garbage collectors in between each as follows,
foo(1) del foo(1) gc.collect() foo(2) del foo(1) gc.collect() foo(3) del foo(1) gc.collect() foo(4),
still nothing. I was wondering if anybody can recommend a fix to this. Provided is my test code
def get_path(self, start, path=[]):
path.append(start)
my_path = self.dfs_search(start, path)
return my_path
def dfs_search(self, start, path, visited=set()):
for edge in self.vertices[start]:
if edge[0] not in visited:
visited.add(edge[0])
path.append(edge[0])
for i in self.vertices[edge[0]]:
self.dfs_search(edge[0], path, visited)
return path
# Simple graph using non-Nodes
edge1 = Edge('Tacoma', 'Olympia', 5)
edge2 = Edge('Olympia', 'Enumclaw', 3)
edge3 = Edge('Enumclaw', 'Silverdale', 11)
edge4 = Edge('Silverdale', 'Seattle', 2)
edge5 = Edge('Seattle', 'Tacoma', 7)
edge6 = Edge('Tacoma', 'Silverdale', 20)
edge7 = Edge('Seattle', 'Olympia', 8)
edge8 = Edge('Puyallup', 'Bremerton', 1)
edge9 = Edge('Bremerton', 'Lacey', 16)
edge10 = Edge('Poulsbo', 'Lacey', 3)
edges = [edge1, edge2, edge3, edge4, edge5, edge6, edge7, edge8, edge9, edge10]
graph = Graph()
for edge in edges:
graph.add_edges(edge, is_directed=True)
tac_path = graph.get_path('Tacoma')
print(tac_path)
puyallup_path = graph.get_path('Puyallup')
print(puyallup_path)
del puyallup_path
gc.collect()
poulsbo_path = graph.get_path('Poulsbo')
print(poulsbo_path)
if __name__ == '__main__':
main()
So as you can see, focusing on edge8, edge9, edge10, 'Poulsbo' should not have a connection to 'Puyallup' or 'Bremerton'. If I comment out puyallup_path, I get the expected output of Poulsbo --> Lacey. However, if I leave puyallup_path uncommented, then poulsbo_path now returns Puyallup --> Bremerton --> Lacey --> Poulsbo
NOTE -- I am practicing on tightening up on my graph theory implementation, so I am still currently looking for ways to make the code more efficient, so that is not my priority in regards to this specific question. This question is more specifically concerned with proper argument passing. Thanks all.