ClassicComputerScienceProblemsInPython
ClassicComputerScienceProblemsInPython copied to clipboard
Mutable Default Arguments
The constructor of the Graph
class has []
as the default value for the vertices
parameter. However, this implementation would fail the following test code.
class TestGraph(unittest.TestCase):
def setUp(self):
self.graph = Graph()
def test_add_vertex(self):
self.assertEqual(self.graph.vertex_count, 0)
def test_remove_vertex(self):
self.graph.add_vertex('A')
self.graph.add_vertex('B')
self.graph.add_vertex('C')
self.assertEqual(self.graph.vertex_count, 3)
The reason for this issue is explained in detail in the following link: