0

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?

Jo Mo
  • 163
  • 7
  • 1
    @jonrsharpe: I guess. No reason to downvote, tho... nothing was listed in the "similar questions". – Jo Mo Dec 07 '21 at 13:37

0 Answers0