Mutable Default Arguments in Python

Mutable-Default-Arguments
Credit: codingstreets

Overview: 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? – 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 add_item(item, items=[]):
    items.append(item) 
    return items

print(add_item(1)) 
print(add_item(2)) 
print(add_item(3)) 
print(add_item(8,[4,9]))
				
			

Explanation: Here variable items stored the Mutable Default Arguments that is an empty list and is defined with function, meaning if the items’s value is not specified, then function considers empty list [] as its value.

On each call, using the append() list method, the item parameter value is added to the default argument, i.e, an empty list and keeps using the same modified list on each call.

On each call, the function uses the same modified list with the existing values in the list rather than creating a new list for each output.

Why This Happens

The default list items=[] is created only once when the function is defined

The same list object is reused in every subsequent function call

Each call appends to this persistent list rather than creating a new one

Mutable Default Arguments – Solution

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

print(add_item(1)) 
print(add_item(2)) 
print(add_item(3)) 
print(add_item(4, 5))
				
			

Explanation:  Here variable items is assigned to None when the function is defined. Inside the main function body – condition is defined to check if items equal to None, if it is then an empty list (mutable object) is assigned to items and using append() method the value of item variable is added to the list.

On each call, items is None, therefore the value of items is rest on each call and a new fresh list is created to add output of each call after checking the conditional statement.

At the last call, items is assigned with a value 5, which makes the condition False of having None; therefore, the conditional statement block of code is not executed and only 5 is returned as items’s value.

Key Points to Remember

Default arguments are evaluated only once – when the function is defined, not each time it’s called.

Never use mutable defaults (lists, dicts, sets) as default arguments.

Use None as a default and create a new mutable object inside the function.

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: 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.

Recent Articles