← Back

Home β€Ί Problems β€Ί Mutable default argument trap

Mutable default argument trap

🧠 Predict the output Topic: functions Difficulty: medium Free

What this problem practises

Function bugs are mostly about names and values: positional argument order, default parameters evaluated once at definition time, local versus global scope, and the implicit None returned whenever a function has no return.

This is an output-prediction problem: read the snippet carefully and write down exactly what it prints before anything runs. These questions are interview staples because they test whether you know the language rules, or merely recognise their syntax.

Difficulty: medium β€” expect to hold two interacting rules in your head at once, such as a boundary combined with a mutation.

The challenge

The code runs in PyDebug’s in-browser compiler. Open the problem, run the snippet, watch the exact error or output, and keep iterating until it is right. Signed-in solves count toward XP and your daily streak.

The snippet

The interactive editor pre-fills exactly this code β€” read it, then submit your prediction:

def add_item(item, items=[]):
    items.append(item)
    return items

print(add_item("A"))
print(add_item("B"))

Understand the rule

Default arguments are evaluated once, when Python creates the function, not every time the function is called. A list used as a default therefore becomes shared state between calls. The function appears to remember old values even though the caller did not pass them explicitly.

How to trace it

  1. Look at the function definition and identify which default objects are mutable.
  2. Call the function once and note that the default list is modified.
  3. Call it again without an argument and observe that the same list is reused.
  4. Replace the default list with None, then create a fresh list inside the function.

Common wrong fixes

Rule to remember

Use None as the default sentinel for a list or dictionary, then allocate the mutable object inside the function. This keeps each call independent.

Solve it in the interactive editor β†’

🧩 Learning path: Functions

Problem 12 of 12 in this path. Work them in order β€” each one builds on the rule before it.

← Modifying a variable before it's assigned locally🏁 Path complete β€” review it β†’

πŸ’Ό Learning path: Interview Practice

Problem 1 of 16 in this path. Work them in order β€” each one builds on the rule before it.

↩ Path overviewNext in path: Class variable shared across instances β†’

More functions problems

Keep going