First, in the Python language, the data takes the form of objects, these objects are a block of memory with values, and a variable is something that references to that piece of memory. The variable stores the address to that object, that is why you can reassign a variable pointing to different types of data.
As we see, everything is an object in Python. Even simple numbers, with values, and supported operations. These objects are classified as immutable or mutable.
id and type
Every object has its own identity that stores its address in memory and has his own type. In order to be able to see these characteristics within the object or instance, we use the built-in function id(), that returns the instance’s memory address of any object. Example:
On the other hand, the type object can be given by the built-in function type(), which returns the type of an object. Let’s look at an example:
Mutable and Immutable objects
At the start of this article, we already said that the Python objects can be classified as mutable (changeable) or immutable (unchangeable). In simple terms, the mutable objects can change their values at any time and place, and the immutable objects can guarantee that the object remains the same constant throughout a program. This distinction is crucial in Python work
lists, dictionaries, and sets, bytearray.
Numbers, complex, bool, strings, tuples, bytes, and frozen sets
Mutable and Immutable objects:
user-defined classes (Can be mutable or be specified as immutable)
Let’s do an example of Mutable objects with a list type object take a look into the address of the list and every object within it:
Now let's change an element of the list for another integer like 5 into the position 2 in the list:
As we could see the address of the list and every object in it, except for position 2, remains the same. However, position 2 now stores a different memory address that holds the value of 5
Now check an example with an immutable object like a string, look the address:
We can try to change a letter in the string, for example, change the position 0 with a ‘J’, but would it work?
Mmmh. That surely doesn’t look good. Trying to reassign a single character in a string results in a TypeError error, as you could the ‘str’ object does not support item assignment.
Why does it matter and how differently does Python treat mutable and immutable objects?
Python uses these objects in different ways. For example, immutable objects are quicker to access since they cannot be modified, but expensive to change because it would involve making a copy to cast into a mutable object. Here are some cases for each type of object that can be seen below:
Mutable objects: Great for efficiently passing data around, when you have to deal with growing data, customed objects, parsing a file, adjusting new conditions, etc. The facility to access information saves a lot of work than if you were using an immutable object.
Immutable objects: Awesome for working with the data. As you know immutable objects are the basic types in Python, such as int, float, and string. Even python’s often-used dict requires keys to be immutable. It is a data structure that relies on some features being guaranteed for its elements.
How arguments are passed to functions and what does that imply for mutable and immutable objects
The arguments are passed to functions by objects in Python. The function call and the definition block or block of the functions share this very same object or variable. If you change the value of a function argument inside the function, the value of the variable changes as well where the function was called.
However, in the case for immutable objects (numbers, tuples, strings, etc), when passed by reference are in reality copying the object, so it can be mutable, but this duplicate is just a local copy of the function that can be manipulated and doesn’t represent any changes to the variable outside the function. For example:
As shown above, the integer value of the variable a, remains unchangeable due to its immutability.
Let’s look at how it works passing a mutable object as an argument to a function. Like with the immutable objects, the mutable objects are passed by reference to the function caller. When the value of this kind of object changes inside the function block, then the value is also changed in the main block. Check an example:
In fact, the list value of the variable l, changes to have a new element at the end of the list.
That's all, for now, hope to have helped you to understand better this concept, see you next time.
- Learning Python from Mark Lutz, chapter 4, Introducing Python Object Types.
- Automate the boring stuff with python from Al Sweigart, chapter 4, Lists, Mutable and immutable data types.