Mutable, Immutable... everything is an object! O.o
Juan Sebastian Gonzalez
Software Developer | Full-Stack Developer | Javascript | React | Python | Nextjs | Nestjs | Fast Api | Docker | MYSQL | MONGO
In Python, everything is an object, which means every entity has some metadata (called?attributes) and associated functionality (called?methods).
Objects in Python
In Python, everything is treated as an object. Every object has these three attributes:
Identity – This refers to the address that the object refers to in the computer’s memory.
Type – This refers to the kind of object that is created. For example- integer, list, string etc.?
Value – This refers to the value stored by the object. For example – List=[1,2,3,4] would hold the numbers 1,2,3 and 3
While ID and Type cannot be changed once it’s created, values can be changed for Mutable objects.
Mutable and Immutable in Python
The simplest definition is: An?object?whose internal state can be changed is?mutable. On the other hand,?immutable?doesn’t allow any change in the object once it has been created.
How do we figure out if our variable is a mutable or immutable object? For this, we should understand what ‘ID’ and ‘TYPE’ functions are for.
ID and TYPE
The built-in function?id() returns the identity of an object as an integer. This integer usually corresponds to the object’s location in memory, although this is specific to the Python implementation and the platform being used. The?is?operator compares the identity of two objects.
The built-in function type() returns the type of an object.
Mutable Definition
Object mutability is one of the characteristics that makes Python a dynamically typed language.
Mutable is when something is changeable or has the ability to change. In Python, mutable is the ability of objects to change their values. These are often the objects that store a collection of data.
Like is shown above we have a list l which has an id = 140254948504256, which is the unique id for the created object and as you can see was possible to change the information inside the list and the id doesn't change.
Even though the append was made, and the list content was changed, the address remains the same.
Mutable objects can be used when you want to allow for any updates (CRUD = U). For example, you have a list of student's names in your school, and that needs to be updated every time a new member joins the class. You can create a mutable list, and it can be updated easily.
Immutable Definition
In Python, if the value of an object cannot be changed over time, then it is known as immutable. Once created, the value of these objects is permanent.
If you try and concentrate on different error messages you have encountered, thrown by the respective IDE; you would be able to identify the immutable objects in Python. For instance, consider the below code & associated error message with it, while trying to change the value of a Tuple at index 0.?
领英推荐
List of Mutable and Immutable objects
Objects of built-in type that are mutable are:
Lists
Sets
Dictionaries
User-Defined Classes?
Objects of built-in type that are immutable are:
Numbers (Integer, Rational, Float, Decimal, Complex & Booleans)
Strings
Tuples
Frozen Sets
User-Defined Classes
Important! Exception in inmutability
Not all immutable objects are strictly immutable by definition. Like you can see in the next picture, actually, it's possible to change an element inside a tuple, although this is just possible because the inside element is in fact a mutable object. So, in this case, there was a change but the type of the tuple is the same.
Objects of built-in types like (int, float, bool, str, tuple, unicode) are immutable. Objects of built-in types like (list, set, dict) are mutable. Custom classes are generally mutable. To simulate immutability in a class, one should override attribute setting and deletion to raise exceptions.
Immutability offers a lot of useful applications to different sensitive tasks we do in a network-centered environment where we allow for parallel processing. By creating immutable objects, you seal the values and ensure that no threads can invoke overwrite/update to your data. This is also useful in situations where you would like to write a piece of code that cannot be modified. For example, a debug code that attempts to find the value of an immutable object.
How objects are passed to Functions
It's important to know the difference between mutable and immutable types and how they are treated when passed onto functions. Memory efficiency is highly affected when the proper objects are used.
When you pass a variable by reference, you are affecting the variable outside of the function, which means that you don't have to return any value, and you will be modifying the original value itself. So, at least that is what you are looking for, is highly suggested make a copy of the variable before do any action.
In the case of Immutable objects, by itself can't be changed so you don't need to take care of them.
In the above's function, after the implementation was returned a value and stored in b. In this case, taking into account that int objects are immutable we really get a completely new object which doesn't affect the input object.
I hope this blog has been useful for the reader, until the next opportunity.
Resources