Everything in python is an object!

Hugo Vargas
5 min readOct 9, 2019

The objects are the must fundamental notion in Python Programming. Everything in python is an object represented by the PyObject Structure in C programming, because the must popular version CPython is written in that language. This fundamental structure PyObject contains:

  1. An struct that define pointers to support a double linked list of all live objects.
  2. A reference count to decide when it can destroyed.
  3. A link to the another PyObject defining the data type.
Python Version 3.4

Since this version of python is written in C, this interacts with the management memory of this language. The magic of python is that we don’t need to worry for this complicated things like pointers or bytes, and python handled all this automatically. All objects are stored in a memory, and depending the type, they can be changed or remain immutable.

To better understand of objects behavior we can play with the id() and type() methods. id() is an inbuilt function that receives an object and return the identity of an object, in a few words a memory address representation. type() is a builtin method that returns the type of the given object.

Here an example of the nature of integer type:

What’s happen here? Well, as we see both variables has the same id address, now let’s try with the type() method;

As we expected, the variable has a type int, but what it is a class? The class is a template of the object and defines the behavior or state of this.

The mystery of the two variables with the same address can be answered as fallows: The integer type don’t works like languages programming like C or C++, as said before everything in python is an object, and this data type is not an exception. Each number is an object and python is smart, to save resources python linked both variables to the same object, this is called aliases. It is a mechanism to refer to a value with different identifiers.

But what happens if we modified one number with an arithmetical operation?

The address changes! This mean that the second variable points to another object. Implicitly this gives a new meaning. Why the value one of the variable “b” cannot be changed? It is because the integer type is a immutable object.

Mutable and Immutable

The objects can have different behaviors and states depending of their nature. Some objects can be modified by changing its value, adding or removing some feature or attribute, and so on. This type are called mutable objects because are more moldable.

For other hand, also exists objects that can’t be changed or modified, and this are called immutable objects. This objects allocate new memory when their value is changed and are faster because remains exactly one state when is created. Therefore, is a thread-safe object and don’t have to deal with multiple threads accessing them currently.

Here a table of data type in python:

The tuples and frozensets are different objects. Basically are immutable lista and frozensets are immutable sets. The frozensets are not indexed and the have functionalities of the sets.

Argument Pass

When a function receives an argument its results can be different depending of its type. The function handles differently while working, and this can have a confusing behavior. With the mutable objects always are passed to functions by reference. The main block and the function share the same object and can be modified from anywhere. Here an example a of mutable object as a list passed in a function:

The object changes like as we expected. Now let’s try with an immutable object as integer:

The number only changes in the scope of the function, but outside still has the same value of the beginning. This happens because both variables work different inside the functions. The mutable objects are passed by reference of the object and the immutable objects are the opposite, its values are passed by assignment of value.

Data Type

Basically, the integer data type is an object as we said. Internally this object is represented by the object PyLong that creates an array of objects (depending on the version) from -5 to 257 define by the macros NSMALLPOSINTS 257and NSMALLNEGINTS 5.

What happens if we use an integer outside of this range?

The values are different because the array has reached its limit and then creates new objects of each new instance. This new object has not limit and is parse from a string to a bytes.

--

--