Mutable Default Argument
PronunciationMYOO-tuh-bul dih-FAWLT AR-gyoo-ment
Definition
A mutable default argument is a default value such as an empty list or dictionary. Python creates it once, when the function is defined, so every call shares the same object.
Where you hear it
In Python interviews, code reviews, and bug hunts where data from one call mysteriously appears in the next.
Examples
The bug was a mutable default argument: every call kept appending to the same list.
Use
Noneas the default and create the list inside the function.The function appended to a default list created once, so old items leaked into new calls.
Common mistake
Writing def add(item, bucket=[]) and expecting a fresh list each time. Use bucket=None and create the list in the body.
Don't confuse with
An immutable default such as a number or a string, which is safe because it cannot be changed in place.
Say it at work
- In a meeting or chat
That's the mutable default argument trap; switch the default to None.
- In an email or ticket
Our linter flags mutable defaults, so please fix it.
Keep exploring
More in Programming Fundamentals