Everything is object- Pyhon

Python is an easy to read and write programming language, it is a language with great documentation so if you want to learn this language from scratch you will surely find a lot of material to support you. But beyond having a reputation of being quite welcoming to beginners, one of its great characteristics is that in Python everything is an object, Functions are objects, classes are objects, even code blocks are objects in a sense … No I can think of nothing in Python that can’t be treated as an object, but everything is an object in the sense that it can be assigned to a variable or passed as an argument to a function.
As we move forward, we may be surprised by many aspects of the Python language, which we may have been unaware of.

What is that we call an object?
We know that an object in Pythhon is something that you can reference a variable to. When we create an object we are instantiating a class and that instance consists of assigning the class as a value to a variable.
Knowing this, let’s start by understanding why everything in Python is an object? Well, it is a system that groups properties and relates them to individual objects.

This sounds more complex than it actually is, let’s see:
Let’s think of objects as part of a system. Let’s think about a program, let’s imagine this program is a company that manufactures a product, in the entire process of the production line, we primarily need a process that is in charge of engaging or processing the materials to obtain the final result, this process can be imagined as the objects in Python that contain data that would be each one of those pieces necessary to achieve the final gear.

So an object becomes the raw material that ends up shaping the application of the program.

At this point we are already being clear why Python represents all its data as objects. Another relevant issue to review when it comes to objects in Python is the issue of immutability and mutability. Some of these objects, such as lists and dictionaries, are mutable, so you can change their content without changing their identity. Other objects such as integers, floats, strings, and tuples are objects that cannot be changed. To understand it quickly it is necessary to review the identification of an object.

A trick to quickly test whether a type is mutable or not is to use the id () built-in function.

we see that the integers are of type mutable so their Id changes.
id and type: Every object has an identity, a type and a value. The identity of an object never changes once it has been created; you can think of it as the address of the object in memory.
When we use the type () function, we can deduce very quickly that the TYPE of the data that we “pass as an argument” is shown to us, between the parentheses.
Let’s see an example:

Mutable and immutable objects:

It is easy to deduce that mutable objects are objects that can be changed while maintaining their identification. In Python, the types that are mutable are: lists, dictionaries, sets, bytearray, etc. A quick and easy way to check if an object is mutable is by using the id () builtin, as we mentioned earlier.

  • Numbers
  • Chains
  • Tuples


When a variable is of an immutable type, such as a string, it is possible to assign a new value to that variable, but it is not possible to modify its content.

This is because when a new assignment is made, the string itself is not modified, but the variable a goes to point to another string. On the other hand, it is not possible to assign a new character in a position, since this would imply modifying the immutable string.

  • Lists
  • Dictionaries
  • Sets

why does it matter and how differently does Python treat mutable and immutable objects:

Mutable objects: When you have a reference to an instance of an object, the content of that instance can be altered
Immutable Objects: When you have a reference to an instance of an object, the content of that instance cannot be altered.

How arguments are passed to functions and what does that imply for mutable and immutable objects:
When the immutable value is passed as a parameter to a function, this value is copied to another memory address, so at first glance we know that if it is modified within the function it will be necessary to extract it to be able to use it outside the function. To put it another way, what happens in the function will not affect variables outside of it.
When we pass a mutable value as a parameter to a function, the variable will point to the mutable object, that is, it is not copied. At this point we can know that when the object is modified inside the function it will be modified outside.