In Python everything is an object!
n Python everything is an object, variables, functions, lists, dictionaries ….
You don’t believe me? Let’s see it.
These checks are done by the built-in function type(), that returns the object type of your argument.
A bit strange, isn’t it? At least for me it was at the beginning, I’ll do my best to make it a little less strange for you. To approach the concept we are going to use one of the simplest and universal things in programming languages, the variables or references or names (synonyms that refer to the same thing).
First, let’s review how it works, we will store a value in a variable.
First we need to store this information somewhere in our computer, for this we have the RAM memory, once this is done we need to be able to access the information, but how does the computer know where it is? here the memory addresses play an important role, an address is a hexadecimal number that tells the computer where to find a value.
So this is how Python works?
This is how some programming languages do it where not everything is taken as an object, but in python it’s a little different because a variable contains a reference which points to an object not to a memory address
How does python find a variable or reference?
The python interpreter generates a random number that allows the computer to find it, these numbers are unique and unrepeatable, they only stop existing to be used again when the object’s reference counter reaches 0. To consult it, python provides us with an internal function called id().
In the previous example we made an assignment, we are going to use the same variable since it still exists in the local variables dictionary of our interpreter, let’s make our example with it.
What is an object?
an object is a collection of data and methods that are created from a class.
To put it a little more simply, think of something, you are the object of the human class.
For the time being, let’s focus on the first part of the definition “an object is a collection of data”.
Let’s see how this works with variables, let’s use the 2 examples above. We have a variable “a” that refers to an id that points to an object composed by the associated value, that is 1, the memory direction and the reference counter. This image will help you to clarify things a bit.
This is how the variable-object association works internally.
Equality and identity
Different variables can point to the same object, for that reason there is the reference counter, let’s see another example.
In this way, the value of the reference counter to the object “300” would not be 1 but 2, because a and b point to the same object.
Let’s do another experiment, could it be that our reference counter increases?
To answer that question it is necessary to see two super important concepts related to objects.
Identity = two identical variables have the same id() identifier so they also have the same content as they point to the same object.
The keyword is used to check the identity of 2 objects.
Equality = two variables are equal if they have the same content but their identifier is different and this is because they do not point to the same object.
The == operator is used to compare the value or equality of two objects
To understand this concept completely, it is necessary to know another very relevant topic in the world of objects.
Mutability and Immutability
Immutability = These are objects that once established cannot change their associated value, if they do so they will only create copies (new objects).
Let’s see an example.
Why is the first id of a different from the second?
300 is a number that belongs to the integer data type which can not be modified, so if you add 1 you are actually creating a new value and therefore a new object.
Mutability
Mutable objects can have modifications in their associated value that do not modify the object’s id, so you are basically working with the same object.
Mutable objects can be equivalent and equal.
Let’s use a list to look at the concept.
As you can see we modified the object to but its id did not change.
Python has predefined mutable and immutable objects
If you find any technical or grammatical error I would appreciate it if you let me know in the comments.