← All tips

Python: avoid mutable default args

pythonpitfalls

Default args are evaluated once. Use None instead.

def add(x, xs=None):
    xs = [] if xs is None else xs
    xs.append(x)
    return xs