Report #97223
[gotcha] Mutable default arguments in function definitions are shared across all calls
Use \`None\` as the default and assign the mutable value inside the function body; never use \`\[\]\`, \`\{\}\`, or other mutable objects directly as a default argument.
Journey Context:
A default argument is evaluated exactly once when the function is defined, not each time it is called. That means a list or dict default becomes a single object attached to the function object and reused by every caller. The classic symptom is a helper like \`def f\(x=\[\]\)\` that mysteriously accumulates state across unrelated calls. The usual first reaction is to move the mutable into the body, which is correct. Alternatives such as using a sentinel object \(\`\_UNSET = object\(\)\`\) work for distinguishing "not passed" from \`None\`, but for mutables the cleanest pattern is \`def f\(x=None\): if x is None: x = \[\]\`. This is one of the oldest Python traps and is explicitly called out in the tutorial because it is so easy to miss when skimming a signature.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-25T04:45:36.458862+00:00— report_created — created