DEV Community

crxs14
crxs14

Posted on

Mutable and Immutable objects

If you’ve just started using Python, so you probably heard the theories that everything in python is objects …

Maybe it sounds weird at the beginning, but the further you dive deep on python, the more you start assimilating and understanding the concept of object thing .

Nevertheless, let’s try today to explore together some basic notions and concepts related to our main topic, how objects are mutable and immutable in Python.

Objects in Python could be either mutable or immutable
Like in science of genetics, the notion of mutation is basically how thing change with the time …Pythonic speaking,a single data object can represent something that evolves independently of the rest of the program. The behavior of a changing object may be influenced by its history, just like an entity in the world.
The concept of mutation in Python could be identified and highlighted by two parameters: id and type …

ID :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.
Type:The built-in function type() and in association with the print function, print the type of an object. Lets look at a simple example :

a = "Holberton_Tunisia"
>>> id(a) = 1258755887 #this is the addressee memory where the              
                       #object is stored
type(a)
>>> <class: 'string'>
Enter fullscreen mode Exit fullscreen mode

why does it matter and how differently does Python treat mutable and immutable objects ?

As we’ve said earlier, a mutation is the ability to change state through the time… So a mutable object can change its state or contents but there is also object that can not mutate … they are immutable .

a = 14 #a variable of type int
b = 14 #a variable of type int
#if we perform some logical operators:
a == b (TRUE)
a is b (TRUE)
#both of the two variables point to the same object
#simple operation:
a = a + 1
a == b (FALSE)
a is b (FALSE)
#modification of immutable objects is not possible
Enter fullscreen mode Exit fullscreen mode

Let’s do the same but with mutable object:

>>> Listx = list([1, 2, 3])
>>> listy = listx
>>> listx.append(2)
>>> print(listx)
>>> 1, 2, 3, 4
>>> print(listy)
>>> 1, 2, 3, 4
#the two objects hast been mutated 
listx == listy (TRUE)
listx is listy (TRUE)
Enter fullscreen mode Exit fullscreen mode

So, generally in Python:

  • Mutable and immutable objects are handled differently . Immutable objects are quicker to access and not possible to change … Whereas mutable objects are easy to change. Use of mutable objects is recommended when there is a need to change the size or content of the object like Tuple . But some exceptions are included here!!! in fact we know that tuple in python is immutable. But the tuple consists of a sequence with unchangeable bindings to objects.

tupl = (6, [3, 4, 5])

The tuple consists of an int and a list. int are immutable so we can’t change its value. But the contents of the list can change. The tuple itself isn’t mutable but contain items that are mutable.

Top comments (0)