#1
Inspired Python
Picture of a coiled snake and the main logo of Inspired Python
def mutable(x, l = []):
    l.append(x)
    print(f"My values {l=}")
>>> mutable("Example 1")
My values l=['Example 1']
>>> mutable("Example 2")
My values l=['Example 1', 'Example 2']

Python Anti-Patterns: Mutable Arguments


Never make a function argument a mutable default, like a list or dictionary. Function arguments are evaluated once on module load. That means you get the same object every time you call the function, like the example above shows.

Be Inspired Get Python tips sent to your inbox

We'll tell you about the latest courses and articles.

Absolutely no spam. We promise!