WHAT ARE OBJECTS? A BRIEF DISCUSSION ON MUTABILITY IN PYTHON

Flavio Espinoza
4 min readJan 16, 2020

INTRODUCTION:

Python is an object oriented language. This means that when coding in python, everything becomes an object which can either be mutable and immutable. To draw further clarification, the type of object defines if it is an immutable or mutable object. All objects also carry information unique to their identification. Once an object is initiated, it carries a unique id that until completion of the code remains the same. The main difference between mutable and immutable objects is whether or not the value of these objects can be changed.

ID AND TYPE:

The id() and type() built-ins offer us with information about the object at hand. They can be implement by calling the function id(a), where a stands for the object you are looking up and returns the location in memory as an int. The type(a) builtin is also implemented by using (a) to stand in for any object you’d like to return a string containing its type. Both these functions are unique in that the type() built-in tells us what type of object we are dealing with, while the id() built-in gives us information about where the object is found in memory.

MUTABLE OBJECTS:

Mutable objects can be of type dict, list, set (generally, custom classes are mutable as well).

The main way you can tell if an object is mutable or not is to test the id of the object at hand before and after an alteration. However, this may not always be true.

To clarify, this means that the state of the object at the memory location given by the address changed to account for the addition of the 4. This will not be the case for immutable as they cannot be edited in the fashion. In the example above a references a list at a specific location, and the edits to that list are executed in the state of the object.

IMMUTABLE OBJECTS:

Immutable objects can be of type bool, float, int, string, tuple or unicode.

As stated above, the main difference between a mutable object and an immutable object is the the change made to the state. For example, when we change the string that we set s equal to, the computer recognizes it as an entirely different string and thus carries a different location in memory.

The state of the object cannot be altered. The object instance s only ever references one point in memory or another depending on the string. This is why it is called an immutable object because while the variable can change the string it contains the address of that variable in the computer changes because the information at the memory address must remain the same.

WHY DOES IT MATTER AND HOW DIFFERENTLY DOES PYTHON TREAT MUTABLE/IMMUTABLE OBJECTS?

To wrap up, the objects that are mutable are of type dict, list, set (and usually your class objects). While the objects that are immutable are of type bool, float, int, string, tuple or unicode. These are key features when writing code as they allow you to set boundaries and manipulate when necessary. Whether intended or not, the value (aka state) of a mutable object may change whereas the value (aka state) of an immutable will remain the same. If by any reason you try to manipulate an immutable object, Python will return an error. However, if a change is made to a mutable object the location in memory will generally stay the same.

HOW ARE ARGUMENTS PASSED INTO FUNCTIONS AND WHAT ARE THE IMPLICATIONS ON MUTABLE/IMMUTABLE OBJECTS?

When python tries to execute a function, the caller code and the function code are called using the same objects. This means that when the code is called, the objects are receive their information by assignment. Mutable objects can be manipulated by the caller code to retain desired information. Immutable objects cannot be changed, but because the caller id has a reference to the information assigned to that immutable it can create another object which can then be manipulated.

--

--

Flavio Espinoza

A software engineering student @ Holberton School.