Python — Everything is object

Drixner
5 min readSep 29, 2022

Introduction

Since we already know that Python is an object-oriented programming language and also that everything in Python is an object, then the main point of discussion here is, first of all, what is an object? So, in object-oriented programming languages such as Python, an object is an entity that contains data along with associated metadata or functionality; this data contained in an object is known as the object’s data attributes. These attributes are simply variables that reference the data. The procedures or series of activities conducted in a certain order or manner that an object performs are known as methods. The methods of an object are functions that perform operations on the objects’ data attributes.

What is an id and a type?

The built-in function id() takes an object as an argument and returns another object that serves as a unique identifier for the first one. What type of object is returned and how it identifies the object passed as an argument depends on the Python implementation. The returned value is an integer that indicates the memory address at which the object is stored.
In Python, almost everything is an object, whether it is a number, a function, or even modules. Python uses a pure object model where classes are instances of a metaclass “type” In Python, the terms “type” and “class” are synonymous. And “type” is the only class that is an instance of itself. This object model can be useful when we want information about a particular resource in Python. if two variables have the same value, python will assign the same address to both.

Mutable and immutable targets:

Mutable objects.

Based on the homonym alone, it can be inferred that mutable objects are objects that can be changed while maintaining their identification. In Python, the types that are mutable are: lists, dictionaries, arrays, bytearray, etc. A quick and easy way to check if an object is mutable is by using id()

In the above example, I created an object of type list “a” with the value 1 in it. After creating the list, I added a 2 to the end of the list. The embedded attachment mutates the list by increasing its length by one and adds its argument to the end of the list. I have taken the ID of object “a” before and after the change and, as you can see, they are the same. This means that object “a” has been modified but kept its unique ID and subsequently its memory address.

Immutable objects

Immutable objects are the direct opposite of mutable objects, since they do not allow changes after their creation. Such types of objects are: integers, floats, complexes, strings, tuples, frozen sets and bytes.

In this example, I have created an object “a” of type int. We know that “a” is immutable, so when we add 1 to “a”, we will have 2 and a different identifier value. I have also taken the identifiers of the numbers themselves and we can see that the identifier of “a” is a direct copy of the integer identifiers. This is expected behavior because within Python, there is an array of integer objects from -5 to 256 preallocated for us. Whenever we refer to a number within this range, we are actually referring to an existing object in memory rather than creating a new object.

Why should I care whether it is immutable or mutable?

Since immutable objects are impervious to change, what happens when we try to concatenate two immutable objects as two strings?

As we can see, the identification of object “a” was changed when we concatenated the string “World” to it. In this operation, the concatenation of “a” and the string created a new object “a” with the type string containing the value of both the old object “a” and the string “World”. This means that “changing” an immutable type is an arduous process since a new copy is needed. This can be a very expensive process for extremely long strings or dictionaries. On the other hand, mutable objects are easier to mutate. However, the inflexibility of immutable types can be useful. Since the actual object cannot be changed unlike mutable objects, we can be sure that the object will remain the same unless an operation is used. However, there is a loophole in this immutable rule. What happens when you add a mutable object inside an immutable object such as a tuple?

Here, I have created an object “a” with the class tuple containing a reference to a string, which is immutable, and a list, which is mutable.

Since the list is mutable, I can use built-in lists to modify the list itself. As we can see, none of the IDs have changed, which means that the list of mutable objects inside the immutable tuple is the same as before. So, what is immutable is the content of the tuple itself, that is, the references to the string and the list. So, this means that the immutable objects inside an immutable object cannot be changed, since the only way to “change” an immutable object is to create a new object with the updated values.

Conclusions

We must always keep in mind that in python everything is an object and therefore must be treated as such, and know how to handle the data depending on its attribute as mutable or immutable, how each type of data is shared and how to use it in the most optimal way possible. Good codes!!!

--

--