The default argument in Python is any object that was specified when defining a function, even if you set a mutable object. This question should explain what this means and why Python is an SO question. The least surprising thing in python is: a mutable default argument .
Basically, the same object is used by default every time a function is called, and not every time a new copy is created. For instance:
>>> def f(xs=[]): ... xs.append(5) ... print xs ... >>> f() [5] >>> f() [5, 5]
The easiest way is to create the actual default argument None , and then just check for None and specify the default value in the function, for example:
>>> def f(xs=None): ... if xs is None: ... xs = [] ... xs.append(5) ... print xs ... >>> f() [5] >>> f() [5]
source share