1

First and foremost, I am a newbie on python. I have a (possibly) trivial question. I have this following code

def foo(a=[]):
   a.append(1)
   print a

foo()
foo()

When I run this code, I get the following:

[1]
[1, 1]

I am wondering why the input parameter keeps expanding. I imagine that once we are done calling the function the first time, the variable a in the foo function is out of scope and is thus thrown to garbage. But instead, I see that this variable persists and keeps expanding every time I call foo().

Can anybody please shed some light on how the variables are managed in this case?

0 Answers0