mutable
In Python, a mutable object allows you to modify its value in place without creating a new object. This characteristic is particularly useful when you need to make frequent changes to data, as it can improve performance by avoiding the overhead of creating new objects. Common mutable data types in Python include lists, dictionaries, and sets.
When working with mutable objects, it’s important to be aware of the implications for your program. For example, if you pass a mutable object to a function, any changes made to the object within the function will affect the original object outside the function. This behavior can be advantageous and requires careful management to avoid unintended side effects.
In multithreaded programs, mutable objects shared between threads also require careful synchronization to avoid race conditions.
In contrast, an immutable object is an object whose state can’t be modified after it’s created.
Example
Here’s a quick example demonstrating the mutability of a list in Python:
>>> numbers = [1, 3, 3]
>>> numbers[1] = 2
>>> numbers
[1, 2, 3]
>>> numbers.append(4)
>>> numbers
[1, 2, 3, 4]
In this example, you use an assignment to change the second value in the list. Then, you use the .append() method to modify the list by adding a new value, demonstrating the mutability of Python lists.
To see that both changes land in the same object, step through the example below. Then switch the object type to tuple to run the same lines against an immutable object:
With a list, each line updates the existing object in place. The changed slots turn yellow, and id(numbers) stays the same throughout. With a tuple, both changes raise an exception, the red border marks the refused change, and the object keeps its original values.
Related Resources
Tutorial
Python's Mutable vs Immutable Types: What's the Difference?
Learn how Python mutable and immutable data types work internally and how you can use mutability or immutability to power your code.
For additional information on related topics, take a look at the following resources:
- Differences Between Python's Mutable and Immutable Types (Course)
- Python's Bytearray: A Mutable Sequence of Bytes (Tutorial)
- Lists vs Tuples in Python (Tutorial)
- Python's list Data Type: A Deep Dive With Examples (Tutorial)
- Python Basics: Lists and Tuples (Course)
- Python's tuple Data Type: A Deep Dive With Examples (Tutorial)
- Strings and Character Data in Python (Tutorial)
- Python's Mutable vs Immutable Types: What's the Difference? (Quiz)
- Python's Bytearray (Quiz)
- Lists and Tuples in Python (Course)
- Lists vs Tuples in Python (Quiz)
- Exploring Python's list Data Type With Examples (Course)
- Python's list Data Type: A Deep Dive With Examples (Quiz)
- Exploring Python's tuple Data Type With Examples (Course)
- Python's tuple Data Type: A Deep Dive With Examples (Quiz)
- Strings and Character Data in Python (Course)
- Python Strings and Character Data (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.