#1
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!