Default Variable Gotcha

Default variable might not work with multiple test cases.

For example we have something like this

class App:
    def searchWord(self, board, row, col, word, 
								    index, visited = []):
        if index >= len(word):
            return True

        if not self.isValidCell(board, row, col, visited): 
            return False

        currentLetter = board[row][col] 
        if currentLetter != word[index]: return False
        
        visited.append((row, col))

        for dr, dc in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            nextRow, nextCol = row + dr, col + dc
            if (self.searchWord(board, nextRow, nextCol, word, index + 1, visited)):
                return True

In here we have the visited=[] denoted inside the searchWord method itself.

However this might cause a bug when dealing with multiple test cases:

class AppTest(unittest.TestCase):
    def setUp(self) -> None:
        self.app = App()
        return super().setUp()

    def test_exist_givenNums_return_result(self):
        actual = self.app.exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCCED")
        self.assertTrue(actual)

        actual = self.app.exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "SEE")
        self.assertTrue(actual)

        actual = self.app.exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCB")
        self.assertFalse(actual)

Note: In all of these test cases here we're sharing the same variable visited and it might be filled in already from the previous test case.

So try not to use default variable if you could