I'm a beginner in Python. If you run the following simple code:
def f(x,l=[]):
for i in range(x):
l.append(i*i)
print(l)
f(2)
f(3,[3,2,1])
f(3)
The output is:
[0, 1]
[3, 2, 1, 0, 1, 4]
[0, 1, 0, 1, 4]
The third output doesn't make sense to me. I found an explanation here, but again not clear. I mean if the list stores the previous value in memory [0, 1]
, why then not store [3, 2, 1, 0, 1, 4]
? According to the explanation mentioned above, the function "uses the original list stored in the original memory block". But we have two [0, 1]
, and [3, 2, 1, 0, 1, 4]
.
How can I say which one is the original?