Fix a function that accidentally shares a list between calls because it uses `items=[]` as a default argument.
Answer
Replace the mutable default with `None` and create a new list inside the function when the caller did not supply one. Default expressions execute once when the function is defined, so a list literal in the signature becomes one shared object. A sentinel avoids that shared state while still allowing callers to pass their own list intentionally.
Strong answer includes
- uses None as a sentinel
- creates the list inside the function
- explains that defaults are evaluated once at definition time
Practical example
Python
Result
Run the code to see the result.
Each omitted `items` argument creates a fresh list during the call rather than reusing one object stored in the function defaults.
Expected result: ['a'] then ['b'].