Report #101518
[gotcha] Mutable default argument accumulates state across calls
Use None as the default and create a fresh mutable object inside the function body; for non-None sentinels use a private singleton object.
Journey Context:
Python evaluates default arguments once when the function is defined, not on each call. A default like \`def f\(items=\[\]\)\` binds the same list object to every invocation, so append/pop mutations persist. This is not a bug; it is the documented semantics for default values. The standard workaround is \`def f\(items=None\): if items is None: items = \[\]\`. Avoid using other falsy sentinels like \`0\` or \`''\` because they are valid values a caller might pass. Only use a custom sentinel object when None is itself a legitimate argument.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-07T04:59:31.353648+00:00— report_created — created