Python3: Mutable, Immutable… everything is object!

Paola Andrea Garcia Altamirano
3 min readOct 7, 2021

--

https://miro.medium.com/max/1000/1*gLNMeKAxC4dPjZwbbT303w.png

In Python there is a great variety of concepts that until today we had not had in our lexicon, but when we start this journey of object-oriented programming we must become familiar with them as they are a great basis for our knowledge.
Among the terms that we must know we have the following:

Identity: It never changes and univocally identifies the object. The operator “is” allows us to know if two objects are actually the same. That is, if two variables refer to the same one.

identity

Type: It indicates the type to which it belongs, such as a float or a list. The type() function tells us the type of a given object. It is the class it belongs to.

type

Value: Every object has particular characteristics. If these characteristics can be modified, we will say that it is a mutable type. Otherwise, it is immutable.

value

These concepts are the basis for understanding more complex concepts such as mutability and immutability; mutable objects are all those that can be modified once created, the types of mutable objects are:
- Lists
- Bytearray
- Memoryview
- Dictionaries
- Sets
- And user-defined classes

And immutable are all those that cannot be modified once created, for example:

  • Booleans
    - Complex
    - Integer
    - Float
    - Frozenset
    - Strings
    - Tuples
    - Range
    - Bytes

The way Python handles mutable and immutable objects is very different, in fact it is easier to access the immutable ones since it is a bigger cost to make a copy than to make a direct change in the object as in the mutable objects you can change some value directly in the object.

The arguments in immutable objects are passed by reference as mutable objects, such as list, set and dict. Because of the state of immutable (immutable) objects, if an integer or string value within the function block is changed, then it behaves like an object. copying process. A new duplicate local copy of the calling object is created and manipulated within the scope of the function block. The caller object will remain unchanged. Therefore, the calling block will not notice any changes made within the scope of the function block to the immutable object.

In mutable objects such as dict and list are also passed by reference. If the value of a mutable object is changed within the scope of the function block, then its value is also changed within the scope of the call or main block, regardless of the argument name. Let’s see the following diagram and code example with the explanation at the end when we assign list1 = list2.

0*NC6zfdCk4weViMW1.png
Python Mutable Function Arguments

Infography:

https://www.google.com/search?q=mutables+vs+inmutables+en+python&tbm=isch&ved=2ahUKEwidqtekurnzAhVNJd8KHc6PBmQQ2-cCegQIABAA&oq=mutables+vs+inmutables+en+python&gs_lcp=CgNpbWcQA1CnqQZYycQGYNPeBmgAcAB4AIABjAGIAfUCkgEDMC4zmAEAoAEBqgELZ3dzLXdpei1pbWfAAQE&sclient=img&ei=_oJfYZ2zNs3K_AbOn5qgBg&bih=722&biw=1536

--

--