I am experimenting with calling a function with different arguments. Now I know from the Python Docs (Python Tutorial 4.7.1. Defining Functions) - that a function accumulates the arguments passed to it on subsequent calls. Because of this after the first call I was expecting the id()
of the list object in my function to remain constant, but it does not. Why are the id()'s
different?
def f(a, L=[]):
print(id(L))
L.append(a)
return L
>>> f(1)
2053668960840
[1]
>>> f(1)
2053668960840
[1, 1]
>>> f(1,[9])
2053668961032
[9, 1]
>>> f(1,[9])
2053669026888
[9, 1]
>>> f(1,[9])
2053668961032
[9, 1]
>>> f(1,[9])
2053669026888
[9, 1]