I had a vague understanding of variable scope in functions in Python, so I'll share it for anyone who stumbled upon something similar. What I want to say is that if you make a whole assignment to a variable prepared as an argument of the __ function, it will be treated as if a new local variable is defined, and if you make an assignment to an element of the variable. Please note that there is a difference that it is treated as a global variable as it is __.
In the code below, we are manipulating two lists list_a and list_b inside the function, but only list_b is treated as a global variable and has an effect outside the function.
def func(list_a, list_b):
list_a = list_a + ['hoge']
list_b[1] = list_b[1] + 10
a = [1, 2, 3]
b = [5, 6, 7]
func(a, b)
print(f"a={a}, b={b}")
func(a, b)
print(f"a={a}, b={b}")
The output will be:
a=[1, 2, 3], b=[5, 16, 7]
a=[1, 2, 3], b=[5, 26, 7]
In Python, an expression of the form "a = ..." in a function is treated as a newly defined local variable a that has scope only inside the __ function. Therefore, in list_a = list_a + ['hoge'] in the function func, the list_a on the left side is treated as a new definition of the local variable list_a (list_a on the right side is given as an argument of the function and is outside the function. Is also a variable with scope). Therefore, it has no effect on a outside the function.
On the other hand, for list_b [1] = list_b [1] + 10, list_b [1] on both sides must be treated as the first element of the __ already defined __ variable list_b. In this example, list_b is an argument of the function func, a variable already defined outside the function. Therefore, the processing by func is reflected in the variable b outside the function.
The same is true for variables of other types such as ndarray and pandas.DataFrame.