ClassicComputerScienceProblemsInPython icon indicating copy to clipboard operation
ClassicComputerScienceProblemsInPython copied to clipboard

Mutable Default Arguments

Open ruddyscent opened this issue 1 year ago • 0 comments

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:

Mutable Default Arguments - Python Guide

ruddyscent avatar Nov 29 '23 14:11 ruddyscent