Overview: Python Mutable Default Arguments
Let’s get started with Mutable Default Arguments in Python. In Python, mutable default arguments like list and dictionaries are evaluated once when the function is defined, not each time when the function is called.
Learn how to deal with Mutable Default Arguments in Python and how to fix that problem.
What’s Next? – Python Mutable Default Arguments
In this article, let’s explore how to deal with Mutable Default Arguments in Python. In this Python beginner guide, we will take a close look at the concept of Mutable Default Arguments in Python.
Table of Contents
Watch Video Explanation
Question: What are Mutable Default Arguments in Python and How to Fix It?
In Python, mutable default arguments like list and dictionaries are evaluated once when the function is defined, not each time when the function is called. It means the same mutable object (list or dictionaries) is used across multiple function calls.
For example, if a function is added to the default list argument, then the same modified list will be used at each time the function is called.
To fix it, use the None keyword as default value of the parameter and define the mutable object inside the function body.
Mutable Default Arguments – Problem
def make_coffee(coffee_name, container=[]): # <- default empty pot
container.append(coffee_name)
return container
print(make_coffee("A"))
print(make_coffee("B"))
print(make_coffee("C"))
Explanation:
1. When the machine is installed (function defined):
Python creates one empty pot ([]) and attaches it permanently to the machine
2. Every time you use it:
First use: make_coffee(“A”) → [“A”] (fresh coffee)
Second use: make_coffee(“B”) → [“A”, “B”] (old coffee remains!)
Third use: make_coffee(“C”) → [“A”, “B”,”C”] (old coffee remains!)
The pot is never emptied between uses
Mutable Default Arguments – Solution
def make_coffee(coffee_name, container=None):
if container is None:
container = [] # <- default empty pot
container.append(coffee_name)
return container
print(make_coffee("A"))
print(make_coffee("B"))
print(make_coffee("C"))
Explanation:
1. When the machine is installed:
It just notes “use no pot by default” (None)
2. Every time you use it:
Checks if you brought your own pot
If not, gives you a brand new empty pot
Always starts fresh unless you specify otherwise
Key Takeaway:
Python’s default arguments are like permanent attachments to your function – they don’t reset between calls. For changeable things like lists/dictionaries, always use None and create a new one inside the function to avoid surprises!
Now it’s YOUR turn!
Let’s share your surprising experience of dealing with mutable default arguments in Python. Even do research on your own example and if you get any problem, do reach to me via comment section of YouTube video: Mutable Default Arguments in Python
Conclusion: Python Mutable Default Arguments
In Python, mutable default arguments (like lists or dictionaries) in function definitions can lead to unexpected behavior because they are evaluated only once—when the function is defined—not each time the function is called.
This means the same mutable object persists across multiple function calls, potentially causing unintended side effects if modified.
For example, if a function appends to a default list argument, subsequent calls will use the already modified list. To avoid this, it’s recommended to use None as the default value and initialize the mutable object inside the function body instead.