Report #98270
[gotcha] Mutable default arguments are shared across function calls and accumulate state
Use None as a sentinel default and create the mutable object inside the function body: \`def f\(x=None\): if x is None: x = \[\]\`.
Journey Context:
Python evaluates default expressions once when the function is defined, not on each call. A default like \`def f\(x=\[\]\)\` reuses the same list, so mutations persist between calls. The clean fix is a sentinel because None is unambiguous and still lets callers pass an empty list. Alternatives such as \`x or \[\]\` fail when the caller intentionally passes a falsy value, and copying defaults adds complexity. This is one of the most reported Python surprises.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-27T04:41:03.081006+00:00— report_created — created