Talking about Objects in Python

Cristian Bedoya Blanco
4 min readSep 30, 2020

“Object-oriented programming offers a sustainable way to write spaghetti code. It lets you accrete programs as a series of patches.” ― Paul Graham

Introduction

In simples words, everything is an object, that represents the paradigm of programming. OOP tries to model, describe, and represent our environment and consider every single element on the universe as an object. This allows the user to have their own methods and attributes without having to re-create them each time. Python is built off of a data structure called the, which is what all the data types inherit from — hence why the language consists of objects.

What is an id and type?

Ids and types are object The id is the identity (unique integer) of an object, type return the type of the object. In the expample bellow, we will see:

and then the output is:

str1 id is: 140503324721984
str1 id is: 140503324721984 and str3 id is: 140503324721984
str2 id is: 140503324552528
num1 id is: 10105088 and num3 id is: 10105088
num2 id is: 140503325106096 and num2 id is: 140503325106096

Now we found some interesting results for example, no matter how many times you declare the same string “Hello” the id is going to be equal. On the other’s hands with integers, we noted that some integer (0- 256) have “reserved” id in memory which mean that an int object is not created

Now let’s go over the type function:

and then the output is:

Type of str1: <class 'str'>
Type of num1: <class 'int'>

As we declared type() returned that we expected.

Mutable Objects

A mutable object is an element that you can modify his sub-elements within, several examples of mutable objects are lists, dict, set, byte array.

output:

['cow', 'pig', 'chiken']
['bull', 'pig', 'rooster']

Immutable Objects

An immutable object is an element that you can’t modify his sub-elements within, several examples of mutable objects are int, float, bool, string, Unicode, tuple

Output:

Traceback (most recent call last):
File "/home/vagrant/holberton_projects/holbertonschool-higher_level_programming/0x09-python-everything_is_object/19main.py", line 4, in <module>
tuple1[0] = 4
TypeError: 'tuple' object does not support item assignment

Storage management

Python is High-level-Language that is inspired in Low-level-language “C”, in C you have to worried about memory allocation, python don't concern as much memory, however, is important to know how it’s works .

Python employs a different approach. Instead of storing values in the memory space reserved by the variable, Python has the variable refer to the value. Similar to pointers in C, variables in Python refer to values (or objects) stored somewhere in memory. In fact, all variable names in Python are said to be references to the values, some of which are front-loaded by Python and therefore exist before the name references occur (more on this later). Python keeps an internal counter on how many references an object has. Once the counter goes to zero — meaning that no reference is made to the object — the garbage collector in Python removes the object, thus freeing up the memory.

We can, however, have two variables refer to the same object through a process called “aliasing”: assigning one variable the value of the other variable. In other words, one variable now serves as an alias for the other, since both of them now refer to the same object.

Every time we create a variable that refers to an object, a new object is created except in the next cases:

  1. Some strings
  2. Integers between -5 and 256 (inclusive)
  3. Empty immutable containers “tuples”

It’s due to an implementation, by default these 3 kinds of objects have already created, so Python doesn’t create twice.

The layout of a Python object in the heap. Each box is allocated in a different memory region, and arrows between boxes represent pointers.

Ouput:

10
True
False
False
True
True
True
False

How are arguments passed to functions and what does that imply for mutable and immutable objects?

The way that the Python compiler handles function arguments has to do with whether the objects in the function arguments are mutable or not immutable. If a mutable object is called by reference in a function, the original variable may be changed. If you want to avoid changing the original variable, you need to copy it to another variable. When immutable objects are called by reference in a function, its value cannot be changed.

Output:

1
[1, 2, 3, 4]

As we‘ve known integers are immutable and lists are mutable. So you pass immutable objects as arguments, they are passed by value since they can not be changed. However, if you pass a mutable object argument, you are passing the object as a reference because the objects can be changed. If you want to increment that variable it is necessary to return a value a reassign its value. If you don’t want to affect the list it’s necessary to create a new one.

Preallocation in Python

In Python, upon startup, Python3 keeps an array of integer objects, from -5 to 256. For example, for the int object, macros called NSMALLPOSINTS and NSMALLNEGINTS are used

What does this mean? This means that when you create an int we have 261 (-5 to 256) int object.

--

--

Cristian Bedoya Blanco

Materials Engineer and Software Developer. First that a professional i am humanistic person. I accepts and overcomes every single challenge.