Report #101052
[gotcha] Mutable default argument values are shared across function calls
Use None as the 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. This means a list or dict default becomes a shared attribute of the function object. Developers often discover this when state leaks between calls or when two callers mutate the 'same' default. The alternative of using a sentinel class is overkill; None plus an explicit None-check is the idiomatic, safe pattern. This is documented as an 'Important warning' in the tutorial because it is one of the most common silent bugs in Python code.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-06T04:54:35.530251+00:00— report_created — created