I saw this blog post showing an interesting Python memory block behavior. I read through the explanation but still not sure.
def f(x,l=[]):
for i in range(x):
l.append(i*i)
print(l)
f(2)
f(3)
Prints
[0, 1]
[0, 1, 0, 1, 4]
Why not these below? After finishing f(2)
where does l
get cached? I thought all the local variables would get wiped out.
[0, 1]
[0, 1, 4]