Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
Default values are evaluated once, when the function is defined, so every call shares the same list; use None as the default and create a new list inside the function.
Python evaluates default argument expressions a single time at definition and stores the resulting object on the function. A mutable default like [] or {} is therefore shared between all calls that rely on it, so items from earlier calls leak into later ones. The standard fix is items=None and then if items is None: items = [] inside the body. The same rule explains why defaults like datetime.now() are frozen at definition time.