MemotivaCommon pitfalls and best practices

What is the mutable default argument trap?

Common pitfalls and best practices

Audio flashcard · 0:14

Nortren·

What is the mutable default argument trap?

0:14

When a function parameter has a mutable default like an empty list, the same object is shared across all calls. Modifications persist between calls, leading to surprising behavior. The standard fix is to use None as the default and create the mutable object inside the function body.
docs.python.org