I was going through https://docs.python.org/3.5/tutorial/controlflow.html#default-argument-values. I modified the example there a little bit as below:
x = [4,5]
def f(a, L=x):
L.append(a)
return L
x = [8,9]
print(f(1,[6,7]))
print(f(2))
print(f(3))
The output I got was:
[6, 7, 1]
[4, 5, 2]
[4, 5, 2, 3]
Now, as per what I understood from the Python docs:
The default values are evaluated at the point of function definition in the defining scope. So, L = [4,5]
After the 1st call to f(), L = [6,7,1]. This is fine.
What don't understand is output after 2nd call to f(). If L's value is shared between the calls to f(), then the output after 2nd function call should be [6,7,1,2]. L's value was shared between 2nd and 3rd call to f() but not between 1st and 2nd call.