Dev HelpersOpen library
← Back

Avoid mutable default arguments

Avoid mutable default arguments. A self-contained example using the Python standard library.

Open this example in the interactive library

Source code

def append_value(value, items=None):
    items = [] if items is None else items
    items.append(value)
    return items
print(append_value(1), append_value(2))

Sample output

[1] [2]