Everything is an object in Python

From the integers to the classes and functions, really ….. everything is an object in Python.


If you are not familiarized with the concept of Object Oriented Programming (OOP) and use Python, I have news for you: you have been using objects all this time. Don’t worry, you can still use Python in a procedural way, without ever knowing what a class is, but, it is always good to know how things work, right?

If you don’t know about OOP here are some words you’ll need to understand a little in order to read this article (this words are just but a scratch in the OOP concept):

Class: provides a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made.

Instance: is basically an object of a class, each time a new object is created, it is called an instance of a class.

Method: it is a function inside a class that uses an instance of the class as an argument.

Atrribute: it is a variable that belongs to each instance.

What does it mean that everything is an object?

It means that each variable, or function, whatever, you name it, has it’s own class, that said, a variable type int is an instance of the class int.

Each class has it’s own methods, i. e. the copy() is a method from the class list, also each instance has its own attributes, i. e. the attribute __doc__ from a function has the doc string previously defined in the function source code.

How do I know wich class is an object instance of?

Easy, with a function: type(object), this function receives an object as an argument and returns the class type of it, for example:

type() used with a tuple.

Every object in Python, so, that is, everything, is stored in memory and have a unique identity, this id is given when the object is instanciated, is unique for each object and lasts during the lifetime of a program or the interactive mode, but it will be different each time you run a program, (except for some objects that have a constant unique id, like integers from -5 to 256, this will be explained later on this article), to get the id of a function you can use the id() builtin function, it will return the id of the object:

id() on the tuple already created.

Types of objects:

In Python there are two types of object: those who can change and those who cannot, this are mutable and immutable objects:

Immutable objects:

These are mostly builtin types like int, string, tuple, among others, these objects can’t change its state or value after they have been instanciated, for example, you can’t change the value of a string once it is created:

String objects are immutable in Python.

(Note: a tuple is an immutable object, that said, it is often called container because it has other objects inside it, these objects can be mutable or immutable depending of their type, but the tuple itself, the container, is always immutable, you can’t add another object to a two items tuple.)

Mutable objects:

These are of type list, dict, set, and also custom classes are generally mutable, these objects can change its state and values after they have been created:

List objects are mutable in Python.

Mutable vs Immutable objects:

You can say: “but I can change the value of an int or a string when I do an addition!”, of course you can, but let’s see what happens when we change the value of an integer after it has been created:

Integer objects are immutable

Did you see that the id’s are not the same?, that is because an entirely new integer object was instanciated, Python doesn’t change the value, bacause it can’t, it does create a new instance and assignates it to the name of the variable, the same would happen in an addition.

Now let’s see a mutable object, for example a list:

List objects are mutable.

The id after the addition its the same than before, that means that the object is not a new instance, it is the same object but mutated.

How are objects passed to a function in Python?

It depends, if the object is immutable, a copy of it is passed (passed by value), on the contrary, if an object is mutable, it is passed by reference, it means it is not a copy but the same object, so in the first case, we wouldn’t mess with the original object, in the second, we can change it and it will change across all the scopes in the program.

Immutable objects are passed by value.
Mutable objects are passed by reference.

Now you know a little more about how Python and his ‘everything is object’ dilemma works, but last but not least, remember that you read that integers from -5 to 257 always will have the same id, even when the program is restarted?, that is because that range of numbers is always loaded in memory when a program is executed, when we create an int in that range, we are just doing a reference to an object already created in memory, why? well I don’t know, but I assume it has to do with the fact that those are the most common used numbers so in doing that the execution will be faster.

Integers from -5 to 257 don’t change id.

Thank you for reading and share this if you liked it!.