Report #102989
[gotcha] Mutable default argument values are shared across all function calls
Use None \(or another sentinel\) as the default and initialize the mutable object inside the function body.
Journey Context:
Python evaluates default arguments once at function definition time, not at each call. A list or dict default becomes a single shared object attached to the function object. The common mistake is writing \`def f\(x=\[\]\)\` and appending to \`x\`, which causes state to leak between unrelated calls. Alternatives like using a tuple or frozen set as default avoid mutation but are too restrictive for most APIs. The sentinel pattern \(\`x=None\` then \`if x is None: x = \[\]\`\) is explicit, works with \`argparse\`/\`dataclasses\` style code, and makes the "fresh object per call" intent obvious.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-10T04:49:47.719948+00:00— report_created — created