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)
In [3]:
b = append_to('world')
print(b)
In [4]:
a
Out[4]:
In [5]:
a is b
Out[5]:
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)
In [8]:
d = append_to('world')
print(d)
In [9]:
c
Out[9]: