r/pythonhelp • • Jan 03 '23

Modifying an object through another function

I'm looking at this LeetCode solution and there's something I don't understand about it. The dfs function doesn't return anything. How does the matrix stay updated outside the dfs function if it doesn't return the newly modified list?

Thanks ahead!

0 Upvotes

1 comment sorted by

3

u/carcigenicate Jan 03 '23 edited Jan 03 '23

Note the line:

grid[i][j] = '#'

That's a mutative operation, so dfs is a mutative function. It alters the object given to it.

This is a simpler example of the same idea:

x = []

def func(param):
    param.append(1)

func(x)
print(x)  # [1], not []

append modifies the object passed to the function.