An Object World
Since the moment you start learning Python, the most common phrase you read everywhere is : “in Python everything is an object”, and although it's a nice phrase, it's not very useful if you don´t understand the Object concept. So, to start, what is an object?
An object is an instance of a Class that can contain data and functions of its own. From a float to a string or a boolean, everything assumes the form of an object in Python. In other words, Python lets you assign almost anything to a variable.
But as you can imagine, there are different types of objects. With the type () function you can get the type if you pass the object as an argument:
a = 0
type(a)
< class ‘int’ >
Objects also have an id, which is a unique number that is assigned to the object. To know the id of the object you can use the built in function id ():
id (a)
>>>97890097
Objects can also be divided into Mutable and Immutable. Mutables are those that can be changed after their creation, and Inmutables are those that can't be changed.
If you try to change a string for example, this is what you get:
e = “Ezequiel”
e [0] = “A”
>>> (Traceback most recent call last):
File “ <stdin> “, line 1, in <module>
TypeError: ‘str’ object does not support item assignment
It returns an error since, being a string, it cannot be modified.
But, if we do the following:
e = “Ezequiel”
id(e)
>>> 155787611
e += “is a Holberton student”
print(e)
>>> Ezequiel is a Holberton student
Recommended by LinkedIn
id (e)
15583783700
In this case you may think that the ‘e’ object has been modified. But no. What actually happened is that Python created a new object, with a different id, and assigned it to ‘e’.
Let's see a similar example:
a = 11
id(a)
>>> 1676716766
print(a)
>>> 11
a += 1
print(a)
>>> 12
id(a)
>>> 1676800911
In this example it seems that an integer variable has been modified, but in fact, Python is just pointing our variable ‘a’ to another memory address in which that int, (12), is located.
From this example we could take a general conclusion, which is that, in this language, variables are references to objects. Variables store the memory address of the actual object, very similar to a pointer in C programming language.
The difference between the way Python deals with mutable and immutable objects, is very important specially when passing objects as an argument to a function, lets see an example:
def subtraction (i):
i -= 1
e = 2
subtraction(e)
print(e)
>>> 2
So… why does the ‘e’ variable stay the same? … Exactly, because is an integer, therefore, is an immutable variable.
To conclude, it's very important to know how different Python treats mutable and immutable types when passed into functions, especially to ensure efficiency, both in usability and memory.
Mutable objects are more recommended when you constantly need to change the properties or value of those objects, and; on the contrary, immutable objects are better when you need a stable variable, with a constant value and a fast access to it.