Mutable, Immutable objects.

Python — Everything is object:

Alexander Rodriguez Pineda
4 min readMay 22, 2021

--

Object Oriented Programming (OOP or OOP) is a programming paradigm, Python is a language OOP — what does that mean? It means the Python programming language consists of objects, which allows the user to have their own methods and attributes without having to re-create them each time. The data structure of Python it is called Pyobject which is what all the data types inherit from. That is the reason of everything is an object in Python.

To better understand what is related to OOP, you must have knowledge of mutable and immutable objects, to have the notion of how Python works more in depth, so…

id( ) function

according to python official page documentation this the id definition of function id:

The id() function returns a unique id for the specified object.

All objects in Python has its own unique id.

The id is assigned to the object when it is created.

The id is the object’s memory address, and will be different for each time you run the program. (except for some object that has a constant unique id, like integers from -5 to 256)

example:

output

as we can see this value is the memory address of the object and will be different every time you run the program

type( ) function

The type() method returns class type of the argument(object) passed as parameter. type() function is mostly used for debugging purposes..

type( ) With a Single Object Parameter

If a single object is passed to type(), the function returns its type.

output:

returns class type of the argument(object) passed as parameter.

Mutable objects

Mutable objects are objects whose values can change, the types of mutable objects are:

  1. Sequences: list(), bytearray()
  2. Set type: set()
  3. Mapping type: dict()
  4. Classes, class instances.
  5. etc.

The best way to check if an object mutates or not is checking the id’s, so in the next examples we have two cases operating strings:

output:

The two strings at the beginning have different addresses, but using ‘ob1 += ob2’ (ob1 = ob1 + ob2) to concatenate these strings, the object ob1change its address by a new one, it is that way because a new object is created with the same name but a new address.

Immutable objects

These are objects that can not be manipulated, like:

  1. Numbers: int(), float(), complex()
  2. Sequences: str(), tuple(), frozenset(), bytes()

Lets to define a tuple, a next to that we try to modify the tuple to prove the immutable objects

The tuple has not that attribute because it is defined as immutable. However, for tuples and frozen sets, even though they are immutable objects, Python handles them the same way as mutable since they may contain mutable objects.

How objects are to functions

Its important for us to know 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.

For example if a mutable object is called by reference in a function, it can change the original variable itself. Hence to avoid this, the original variable needs to be copied to another variable. Immutable objects can be called by reference because its value cannot be changed anyways.

def updateList(list1):
list1 += [10]n = [5, 6]
print(id(n)) # 140312184155336updateList(n)
print(n) # [5, 6, 10]
print(id(n)) # 140312184155336

As we can see from the above example, we have called the list via call by reference, so the changes are made to the original list itself.

--

--