In first function you update existing list
, when you append to the end of list new values.
In second function, you just reassign value to list, which means that you didn't add values that list, but just, lets say, recreate it with same value each time function called.
In both cases, you create local variable for function, which appended to that function clouser
. So, in both cases you have local variable, which live in function scope. When you run your function 1-st time, in case of default value of variable, it will be memorized, and later, in case of executing function again, it will not be reinitialized by function, but would be taken from function scope.
For example, try to update code of g
function to next one:
def g(a, L=[]):
L += [2]
print(L)
There you will see, that L
accumulated too, as in f
function, because here you not reassign the value, but update existing variable.