Python / Editorial solution guide
Diagnose shared mutable default arguments
Find state unexpectedly retained between calls and repair the ownership contract.
Read-only knowledge base ยท No public comments
Recognize the symptoms
Calls that omit a list or dictionary argument see items from earlier calls, and isolated tests may pass while a suite fails in a different order.
Understand the likely causes
- A default expression is evaluated when the function is defined, so a mutable object can be reused and changed by later calls. Shared caller-owned objects can produce a similar symptom and require separate investigation.
1. Reproduce in one process
Use a small synthetic example and call the function twice without the optional argument. The second call below sees the first item because both calls use the same default list. Run both calls in the same process: starting a new process for each call constructs a new function and can conceal the symptom. Preserve the call order and expected values in a regression test rather than using production data.
def add_label(label, labels=[]):
labels.append(label)
return labels
print(add_label("red"))
print(add_label("blue"))
['red']
['red', 'blue']
The default list is reused by both calls.
2. Locate the owner
Inspect the function signature and every mutation of the argument. Ask whether the calls omitted it or explicitly passed the same collection. The default is only implicated in the omitted case; callers can deliberately share an explicit list. Also inspect nested mutable objects, because a shallow copy creates a new outer collection while preserving references to its contents. Avoid printing real records when tracing identity or ownership.
3. Repair the intended contract
When omitted calls need independent lists, use None as the default and allocate a list inside the function. Check with is None, not a truthiness test: an explicitly supplied empty list is a valid object that the caller may expect to be updated. This change retains the behavior for explicitly supplied lists; if callers should never observe mutation, design and document a separate copying policy.
def add_label(label, labels=None):
if labels is None:
labels = []
labels.append(label)
return labels
print(add_label("red"))
print(add_label("blue"))
['red']
['blue']
Each omitted argument receives a newly created list.
4. Verify independent and explicit calls
Repeat the original sequence in one process and assert that each omitted-argument result contains only its own item. Next pass an explicit empty list and check both its content and the returned object according to the documented contract. Add a nested collection case if the function copies input, because outer independence alone does not prove inner independence. Run the relevant existing tests and investigate any consumer that relied on accumulated state.
Verify the fix
Two omitted-argument calls are independent, while explicit arguments follow the documented mutation policy. The original failure and the focused regression test pass in the same execution context.
Keep it from recurring
- Review mutable defaults and ownership at API boundaries. Test consecutive calls together and include an explicitly supplied empty collection.