You were mutating the arguments thats why, for example see this
def add_to(num, target=[]):
target.append(num)
return target
add_to(1)
# Output: [1]
add_to(2)
# Output: [1, 2]
add_to(3)
# Output: [1, 2, 3]
You might be expecting that a fresh list would be created when you call add_to
, but thats not the case. You mutate the argument, this is what you want to do when you don't want it to mutate the argument
from copy import deepcopy
def foo(some_var):
some_var_ = deepcopy(some_var)
...
return some_var_
Here you mutate the copy of some_var
instead of the original.
You can see here what are the other mutable things. https://book.pythontips.com/en/latest/mutation.html#mutation