Esteban Castaño Zapata
4 min readJan 13, 2021

--

Python3: Mutable, Immutable… everything is object!

Introduction: What is an object and what does it means everything in python is an object?

An object is something that can store stuff. From the point of view of a programming language, the stuff in question is data and functions. So for example, we can think of different houses as objects, like your parents house or your own house, and to help define this objects, we used something call a class.

A class is a template with all the information necessary, it includes attributes, like floor area, number of bedrooms and so forth, an also can include methods or functions, like you want to know the depreciation of the house in a number of years, it can all be in there.

So what is an object in python? Everything, like numbers, functions, strings, etc., meaning that everything in python behaves like an object the is derived from a class. That doesn’t mean that python its strictly an oriented programming language, it just an optional way to do your program.

Some built-in functions in python helps to recognize what type of object is been handle and are explain later.

id and type

The id() function return an identification number of the object. This helps you check if an object has change after performing some operation. For example, lets say we have a list with the inner solar system planets

Using the id function, we can see the identifier number, like so

To see how can this function help us, lets say, we want to add jupiter to our list, we can do it in different ways, like this:

Using the += method, it add the new element to the existing object, its not a new one and we can checked it applying id().

Now if we do the following:

Here we are using two objects, planets and [“Jupiter”], creating a new one, also called planets, and we can check with id() that we are dealing we two different objects.

Another very useful tool available is called type(), this allowed to know what type of object its been handle. A few examples of how type() works are shown in the following image:

Here a its an object of type int (a number) while b is a tuple.

As far as how an user can manipulate an object, its necessary to be aware that there are basically to cases, in can be mutable and immutable and it is explain next.

Mutable objects

This kind of objects are those whose elements can be changed an remain been the same object. In the following example, we use a list to show how a mutable object works

In this case, the first element can be change for a new value.

Immutable objects

On the other hand, we have immutable objects in which you cannot change the value store in the object. To show the difference, we use a tuple as an examples:

Here you can obtain the values of the tuple, like a list, but if you try to change it, you see that this is not possible, because tuples, like strings, are immutable objects.

That doesn’t mean that the tuple cannot be change in any way, even if the values store cannot be change, its possible to do others operations to modified, like adding new data, as shown:

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

This is important from a user perspective, because there is information that you may want to change in the course of the program but there is also stuff you don’t want to be change by accident.

How arguments are passed to functions and what does that imply for mutable and immutable objects?

Arguments are passed to function by reference in both mutable and immutable objects, the difference is that the value of those immutable objects is not change if the value is change inside the function.

--

--

Esteban Castaño Zapata
0 Followers

Aspiring software engineer at Holberton School