Consider the following function
def buggy(n, l=[]):
l.append(n)
if n == 1: return l
return buggy(n-1, l)
In my understanding, the expected behaviour is:
>>> buggy(2)
[2,1]
>>> buggy(2)
[2,1]
>>> buggy(2)
[2,1]
But for some reason, successive calls to buggy append to the results of previous calls, i.e. my idle looks like this:
>>> buggy(2)
[2,1]
>>> buggy(2)
[2,1,2,1]
>>> buggy(2)
[2,1,2,1,2,1]
It seems like l is not local to the function, which does not feel correct to me.
Is this really what should happen? If so, why? If not, is it a bug?