Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
Python interview question
Functions & functional toolsJuniorVery commonPractical

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

Use None as the sentinel
Python
editable · browser sandbox
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'].

#functions#mutability#gotchas

Sources

The Python TutorialPython Software Foundation · Baseline syntax, control flow, functions and module basicsThe Python Language ReferencePython Software Foundation · Precise scoping, evaluation order and expression semantics