Python / Core Python Fundamentals Interview Questions
What is the mutable default argument trap in Python and how do you fix it?
One of Python's most notorious gotchas: default argument values are evaluated once at function definition time, not each time the function is called. If that default is a mutable object like a list or dict, every call that uses the default shares the same object, producing surprising accumulated state.
# BUG: the list is created once and shared across calls
def add_item(item, collection=[]):
collection.append(item)
return collection
print(add_item('a')) # ['a'] — looks fine
print(add_item('b')) # ['a', 'b'] — surprise! shared default
print(add_item('c')) # ['a', 'b', 'c']The standard fix is to use None as the default and create a fresh object inside the function body:
def add_item(item, collection=None):
if collection is None:
collection = [] # fresh list on every call
collection.append(item)
return collection
print(add_item('a')) # ['a']
print(add_item('b')) # ['b'] — independentThis issue only affects mutable objects (lists, dicts, sets, custom objects). Immutable defaults like integers, strings, and tuples are safe because they cannot be modified in place. You can inspect the current value of a function's defaults at runtime via function.__defaults__, which makes the problem visible: you will see the accumulated list growing there.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
