I am confused about python's evaluation of default function arguments. As mentioned in the documentation (https://docs.python.org/3.6/tutorial/controlflow.html#more-on-defining-functions)
def f(a, L=[]):
L.append(a)
return L
print(f(1))
print(f(2))
print(f(3))
results in
[1]
[1,2]
[1,2,3]
But
def f(a, L=[]):
L = L+[a]
return L
print(f(1))
print(f(2))
print(f(3))
results in
[1]
[2]
[3]
If L is evaluated only once, shouldn't both the function return the same result? Can someone explain what am I missing here?
This question is different than "Least Astonishment" and the Mutable Default Argument. That question is questioning the design choice while here I am trying to understand a specific case.