Python handles the mutability and immutability of objects when passed as function arguments in a specific manner, which is important for understanding how the language operates. In Python, objects can be classified into two categories: mutable and immutable. Mutable objects can be modified after they are created, while immutable objects cannot be changed once they are created.
When an object is passed as an argument to a function in Python, the behavior depends on whether the object is mutable or immutable. If the object is immutable, such as numbers, strings, or tuples, the function receives a copy of the object's value. This means that any modifications made to the object within the function will not affect the original object outside of the function. For example:
python
def modify_immutable(arg):
arg += " World" # Modifying the string argument
print(arg) # Output: Hello World
my_string = "Hello"
modify_immutable(my_string)
print(my_string) # Output: Hello
In the above example, the `modify_immutable` function receives a copy of the string object "Hello". When the function modifies the argument by concatenating " World" to it, it creates a new string object "Hello World". However, this modification does not affect the original string object "Hello" outside of the function.
On the other hand, if the object is mutable, such as lists or dictionaries, the function receives a reference to the original object. This means that any modifications made to the object within the function will affect the original object outside of the function. For example:
python
def modify_mutable(arg):
arg.append("World") # Modifying the list argument
print(arg) # Output: ['Hello', 'World']
my_list = ['Hello']
modify_mutable(my_list)
print(my_list) # Output: ['Hello', 'World']
In this case, the `modify_mutable` function receives a reference to the list object ['Hello']. When the function appends "World" to the list, it modifies the original list object. As a result, the modification is reflected in the original list object outside of the function as well.
It is important to note that even though mutable objects can be modified within a function, reassigning the argument to a new object will not affect the original object outside of the function. For example:
python
def reassign_mutable(arg):
arg = [1, 2, 3] # Reassigning the list argument
print(arg) # Output: [1, 2, 3]
my_list = [4, 5, 6]
reassign_mutable(my_list)
print(my_list) # Output: [4, 5, 6]
In this example, the `reassign_mutable` function reassigns the argument to a new list object [1, 2, 3]. However, this reassignment only affects the local variable within the function and does not modify the original list object [4, 5, 6] outside of the function.
Python handles the mutability and immutability of objects when passed as function arguments by providing a copy of the value for immutable objects, ensuring that modifications within the function do not affect the original object. For mutable objects, a reference to the original object is passed, allowing modifications to be applied directly to the original object. However, reassigning the argument to a new object does not affect the original object outside of the function.
Other recent questions and answers regarding Examination review:
- What is type hinting and how can it be used to specify the expected types of function parameters?
- How can we modify a game board within a function by assigning values to specific positions?
- What are default values for function parameters and how can they be specified?
- How can we pass values for function parameters when calling a function?
- What are function parameters in Python and how are they defined?
- Why is it important to choose meaningful parameter names when defining functions in Python?
- What is the purpose of type annotations in Python function parameters?
- How can we pass values to function parameters in Python?
- What are function parameters in Python and how are they used?

