Python3: Mutable, Immutable… everything is object!

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:

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

My Ruby Essentials

relational database planner example

Reflection in Java

Your Power to Bring Fusion-Soniors Wanted!

Working with Dates in Java

REMOTE JITSERVER TECHNOLOGY

Functionality Frontend Prototyping

Sequelize Associations

Login and Register User — Django Rest Framework

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store