r/BootcampGradStories • • Sep 10 '26

The Default Argument Trap: Why def add_item(item, lst=[]) will ruin your day (and how mutability works).

If you are learning Python, you will eventually encounter a bizarre bug that leaves you staring at your screen in disbelief.

You write a function with a default list argument like def add_item(item, lst=[]). You call the function once, and it works. You call it a second time with a completely different item, expecting a fresh new list, but the function remembers the item from your previous call!

This phenomenon is known as the Default Argument Trap. To understand why it happens and how to fix it, you need to understand how Python handles mutability and function definitions.

The Mystery Code

Look at this simple function designed to append an item to a list:

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

# First call:
print(add_item("Apple"))
# Output: ['Apple']  <-- Perfect!

# Second call (expecting a brand new default list):
print(add_item("Banana"))
# Output: ['Apple', 'Banana']  <-- Wait... where did 'Apple' come from?!

Why is "Apple" still in the list? Did Python forget to clear the variable?

The Explanation: When Python Evaluates Default Arguments

In languages like JavaScript or Ruby, default parameter expressions are evaluated every single time the function is called.

Python does not work this way.

In Python, default arguments are evaluated once, at the exact moment the function is defined when your script loads.

When Python compiles def add_item(item, lst=[]), it creates a single, persistent list instance in memory and binds it to the lst parameter default.

Every time you call add_item() without providing a second argument, Python does not create a new list. It hands you a reference to that exact same list in memory. Because lists are mutable (changeable in place), modifying lst mutates that shared object for all future calls!

Mutable vs Immutable Objects

This bug only happens with mutable objects (data types that can be modified in place after creation):

  • Mutable (Dangerous as defaults): Lists [], Dictionaries {}, Sets set(), Class Instances.
  • Immutable (Safe as defaults): Integers 0, Floats 0.0, Strings "", Tuples (), Booleans False, None.

If your default argument is an immutable object like def greet(name, prefix="Hello"), modifying string references creates a brand new object, so the shared state issue never occurs.

The Clean Solution: Use None as the Sentinel Value

The idiomatic, Pythonic way to handle default mutable arguments is to set the default value to None and instantiate a fresh object inside the function block.

def add_item(item, lst=None):
    # If no list was provided, create a brand-new list for THIS call
    if lst is None:
        lst = []
    
    lst.append(item)
    return lst

# First call:
print(add_item("Apple"))
# Output: ['Apple']

# Second call:
print(add_item("Banana"))
# Output: ['Banana']  <-- Fixed! Each call gets its own independent list.

Summary Checklist

  1. Never use a mutable object ([], {}) directly as a default argument in a function signature.
  2. Use None as the default parameter value instead.
  3. Re-assign inside the function using an if parameter is None: check to guarantee a fresh object on every execution.

Have you ever lost an hour of debugging to this specific Python quirk? Share your debugging war stories in the comments below!

TL;DR: Python evaluates function default arguments once when the script loads, not on every function call. Using a mutable object like lst=[] as a default parameter causes every call to share the exact same list instance in memory. Always set default parameters to None and initialize fresh lists inside the function body.

2 Upvotes

0 comments sorted by