Report #68459
[gotcha] Mutable default arguments create shared state across function calls
Use None as the default sentinel and initialize the mutable object inside the function body \(e.g., def f\(x=None\): if x is None: x = \[\]\)
Journey Context:
Default argument values are evaluated once at function definition time and stored in function.\_\_defaults\_\_, not at call time. This means a mutable default like \[\] or \{\} is a single object shared by all calls. This is especially dangerous with cache dictionaries or accumulator lists. The None-check pattern ensures a fresh object per call while allowing explicit argument passing. Note that using other sentinels like object\(\) is sometimes needed when None is a valid value.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T21:23:37.265746+00:00— report_created — created