Report #104360
[gotcha] Default argument values are evaluated once at function definition time, not at call time
Use a sentinel value like \`None\` and conditionally assign a new default inside the function: \`def foo\(x=None\): if x is None: x = \[\]\`.
Journey Context:
This is a classic Python footgun. Many developers assume defaults are re-evaluated on each call. In reality, the default expression \(even a function call like \`time.time\(\)\`\) is evaluated once when the function is defined. For mutable defaults like \`\[\]\` or \`\{\}\`, modifying the default in one call persists to subsequent calls. The sentinel pattern is the standard workaround; it's explicit, avoids surprising behavior, and works for all types. Alternatives like using \`None\` and creating a new object each time are preferred. The design choice prioritizes performance \(avoiding repeated expensive computations\) but trades off intuition.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-08-09T20:02:39.027834+00:00— report_created — created