Why are default values shared between objects in Python?
Python: Why Do Objects Share Default Values in Python?
In this article, we’ll explain why default values are shared between Python objects. First, we need to understand the concepts of variables and objects in Python.
Read more: Python Tutorial
Variables and Objects
In Python, variables are identifiers used to store values. Objects, on the other hand, are entities that store and manipulate data. Each object has a unique identifier (i.e., a memory address), a type, and a value. Variables allow us to reference an object.
Creating and Assigning Python Objects
When we use a statement to create an object and assign it to a variable, Python follows these steps:
1. Create a new object.
2. Assign a reference (memory address) to the newly created object to a variable.
Why objects share default values
In Python, when we define a mutable object as a default value for a function, this default value is created only once, at function definition time. Then, for each function call without specifying an argument, this default value is used. This leads to the phenomenon of shared default values between objects.
Specifically, when we define a function and use a mutable object (such as a list or dictionary) as a default value, this default value is created at function definition time and shared across all function calls. Python uses the same default value object each time the function is called.
Let’s look at an example:
def append_number(num, list1=[]):
list1.append(num)
return list1
print(append_number(1)) # [1]
print(append_number(2)) # [1, 2]
print(append_number(3)) # [1, 2, 3]
In the above example, we define an append_number
function, where list1
is the default value of a mutable object. When append_number(1)
is first called, an empty list is created as the default value and assigned to list1
. Then we append the value 1 to list1
and return list1
. In the next two calls, since no arguments are specified, Python uses the same default value object, so the result after each call retains the previously added value.
Methods for Preventing Sharing Default Values Between Objects
If we want to avoid sharing default values between objects, there are several ways:
1. Use Immutable Objects as Default Values
Since modifying immutable objects creates a new object, each function call uses a different default value object.
def append_number(num, list1=None):
if list1 is None:
list1 = []
list1.append(num)
return list1
print(append_number(1)) # [1]
print(append_number(2)) # [2]
print(append_number(3)) # [3]
In the above example, we set the default value of list1
to None
, and then check inside the function whether it is None
. If it is, we create an empty list and assign it to list1
. This avoids the problem of shared default values between objects.
2. Use immutable types as default values
Objects of immutable types (such as integers and strings) cannot be modified after creation, so they are not affected by shared default values between objects.
def append_number(num, count=0):
return count + num
print(append_number(1)) # 1
print(append_number(2)) # 2
print(append_number(3)) # 3
In the above example, we set the default value of count
to 0, which is an immutable integer type. Each time the function is called, a new count
object is created and calculated based on the passed argument.
3. Using an Empty Mutable Object as the Default Value
If we do need to use a mutable object as the default value and do not want to share the default value between objects, we can use an empty mutable object as the default value and then handle it within the function.
def append_number(num, list1=[]):
if list1 == []:
list1 = [] # Create a new empty list
list1.append(num)
return list1
print(append_number(1)) # [1]
print(append_number(2)) # [2]
print(append_number(3)) # [3]
In the above example, we used an empty list as the default value and handled it inside the function. When the default value of list1
is equal to []
, we use a new empty list.
Summary
Default values are shared between objects in Python because the default value object is created only once when the function is defined and is shared across all function calls. To avoid this, we can use immutable objects as default values, use immutable types as default values, or use empty mutable objects and handle them inside the function. Choosing the right method depends on your specific needs and design.