Why does using `arg=None` fix Python's mutable default argument issue?
arguments, function, mutable, python
Solution
The default value of `a_list` (or any other default value, for that matter) is stored in the function's interiors once it has been initialized and thus can be modified in any way:
>>> def f(x=[]): return x
...
>>> f.func_defaults
([],)
>>> f.func_defaults[0] is f()
True
resp. for Python 3:
>>> def f(x=[]): return x
...
>>> f.__defaults__
([],)
>>> f.__defaults__[0] is f()
True
So the value in `func_defaults` is the same which is as well known inside function (and returned in my example in order to access it from outside.
In other words, what happens when calling `f()` is an implicit `x = f.func_defaults[0]`. If that object is modified subsequently, you'll keep that modification.
In contrast, an assignment inside the function gets always a new `[]`. Any modification will last until the last reference to that `[]` has gone; on the next function call, a new `[]` is created.
In order words again, it is not true that `[]` gets the same object on every execution, but it is (in the case of default argument) only executed once and then preserved.
Problem
I'm at the point in learning Python where I'm dealing with the Mutable Default Argument problem. ``` # BAD: if `a_list` is not passed in, the default will wrongly retain its contents between successive function calls def bad_append(new_item, a_list=[]): a_list.append(new_item) return a_list # GOOD: if `a_list` is not passed in, the default will always correctly be [] def good_append(new_item, a_list=None): if a_list is None: a_list = [] a_list.append(new_item) return a_list ``` I understand that `a_list` is initialized only when the `def` statement is first encountered, and that's why subsequent calls of `bad_append` use the same list object. What I don't understand is why `good_append` works any different. It looks like `a_list` would still be initialized only once; therefore, the `if` statement would only be true on the first invocation of the function, meaning `a_list` would only get reset to `[]` on the first invocation, meaning it would still accumulate all past `new_item` values and still be buggy. Why isn't it? What concept am I missing? How does `a_list` get wiped clean every time `good_append` runs?