A default argument in Python is whatever object was set when the function was defined, even if you set a mutable object. This question should explain what that means and why Python is the SO question least astonishment in python: the mutable default argument.
Basically, the same default object is used every time the function is called, rather than a new copy being made each time. For example:
>>> def f(xs=[]):
... xs.append(5)
... print xs
...
>>> f()
[5]
>>> f()
[5, 5]
The easiest way around this is to make your actual default argument None
, and then simply check for None
and provide a default in the function, for example:
>>> def f(xs=None):
... if xs is None:
... xs = []
... xs.append(5)
... print xs
...
>>> f()
[5]
>>> f()
[5]