I will explain this article so that you can understand it. Please forgive me though I will write the same content. https://qiita.com/yuku_t/items/fd517a4c3d13f6f3de40
You can set default values for arguments in function definitions and class init. At this time, if the type of the argument to which the initial value is given is a mutable type, it may not be good.
Function with initial value(Bad example).py
def foo(bar=[]): #List type is mutable, be careful!
bar.append('baz')
print(bar)
If you do this with the default values
Run.py
>>> foo()
['baz']
>>> foo()
['baz', 'baz'] #that?
The result has changed. The reason for this is that in Python, the default value of a function is allocated memory when the function is defined. In the above, since the append operation was performed twice for the List type created when the function was defined, two "baz" were included.
To clear this problem In other words, if you want to give an initial value to a mutable type argument without any problem, you can allocate memory when calling the function, not when defining the function. That is, the initialization is performed inside the function.
Function with initial value(Good example).py
def foo(bar=None):
if bar is None:
bar = []
bar.append('baz')
return bar
It is okay to give an initial value to the immutable type argument. Reference: https://qiita.com/makotoo2/items/fc3a617882916f9775f5