Looks like that you are creating a new list object in memory by resetting a new value object of L
at the third time it runs, so it stores the the a
variable value in the new list object instead of appending to the old one that has been in memory already, but in step four and since L=[]
has hasn't been changed this time it still points to the same old list object in memory, so step 4 a
variable value gets stored there as well as step one and two.
here is a simple example:
if we modified the code a little bit to debug what's happening here, you will see that the L
variable get's assigned to a new list object in memory at step 3.
def f(a, L=[]):
L.append(a)
print(f'Your function list id: {id(L)}, now gets assigned a value of: {a}')
return L
print(f(1))
print(f(2))
print(f(3, L=[]))
print(f(4))