Back to all fixes
CuratedPython 3.12local

Function's default list/dict argument 'remembers' values between calls

Problem

A function defined with `def f(items=[])` accumulates data across calls — the default object is shared.

Solution

python
Default arguments are evaluated once at definition time, so a mutable default is reused on every call. Use `None` as the sentinel and create a fresh object inside: `def f(items=None): items = [] if items is None else items`. The same applies to `{}` and any other mutable default.