Mutable default arguments are usually a bug that can gobble memory.

For example,


In [1]:
def append_to(element, target=[]):
    target.append(element)
    return target

In [2]:
a = append_to('hello')
print(a)


['hello']

In [3]:
b = append_to('world')
print(b)


['hello', 'world']

In [4]:
a


Out[4]:
['hello', 'world']

In [5]:
a is b


Out[5]:
True

A better way of initializing to mutable data is shown below.


In [6]:
def append_to(element, target=None):
    if target is None:
        target = []
    target.append(element)
    return target

In [7]:
c = append_to('hello')
print(c)


['hello']

In [8]:
d = append_to('world')
print(d)


['world']

In [9]:
c


Out[9]:
['hello']