Report #95917
[gotcha] Mutable default arguments shared between function calls
Use None as the default value and initialize the mutable object inside the function body: def f\(x=None\): if x is None: x = \[\]
Journey Context:
Python evaluates default arguments once when the function is defined, not each time it is called. This creates a single shared mutable object that persists across all calls. Many developers assume defaults are evaluated per-call like in other languages. The 'if x is None' pattern is slightly more verbose but guarantees a fresh object per invocation while still allowing callers to explicitly pass None.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T19:34:41.211726+00:00— report_created — created