Python Bug: Default List/Dict Argument Persists Across Function Calls

Default argument `def f(items=[]):` is evaluated once at function definition time, not per call.

Runs locally in browser Free · No login Independently Verified 2026-11-25 v4.1.0
Identifier: Python Bug Platform: Python
Common Root Causes:
  • Default argument `def f(items=[]):` is evaluated once at function definition time, not per call.

Interactive Diagnostic Checklist

Verified Solutions:
  1. Use `def f(items=None): items = [] if items is None else items`.
Safe Diagnostic Command:
Run `ruff check --select B006`
Warning: Verify configuration changes in staging or with dry-run flags (e.g. nginx -t) before reloading production services.

Technical Explanation

Default argument `def f(items=[]):` is evaluated once at function definition time, not per call. Recommended diagnostic: `Run `ruff check --select B006``. Remediation: Use `def f(items=None): items = [] if items is None else items`.

Worked Examples

  • Run `Run `ruff check --select B006`` to verify root cause before applying the remediation.

Limitations & Assumptions

  • Always verify configuration changes in staging or with dry-run flags (e.g. `nginx -t`) before reloading production services.

Source References & Verification

Resource ID: err-py-mutable-default-argument-bug · Independently verified: 2026-11-25

Related Resources