Report #8732
[gotcha] Mutable default argument shared across function calls
Use None as the default sentinel and initialize the mutable object inside the function body
Journey Context:
Python evaluates default arguments exactly once when the function is defined, not each time it is called. This creates a single shared mutable object \(list, dict, set\) that persists across all calls. The safe pattern is \`def f\(x=None\): if x is None: x = \[\]\`. Alternatives like \`def f\(x=\(\)\):\` for empty tuples work \(immutable\) but are inconsistent with mutable types. This is a language design tradeoff for performance and scoping, but bites everyone once.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T06:17:21.763937+00:00— report_created — created