Everything is Object in Python

You’ve probably heard that everything is an object in Python, but what does that mean?

Python is one of the most popular object-oriented languages, and it means that this language consist of objects, this objects allow us to have our own methods and attributes without having to re-create them each time.

Here is a representation of an object:

Most programming languages provide a keyword called class to create an Object, and python is not an exception to this.

What is id()?

The id() is a function that returns a unique id for the specified object. The id is assigned to the object when it is created, is “unique and constant” for the object as long as you use it.


# Declaring two string objects:>>> obj1 = "Hello"
>>> obj2 = "Alex"
# Getting their ids:>>> id(obj1)
>>> id(obj2)

What is type()?

type() method returns class type of the argument(object) passed as parameter. type() function is mostly used for debugging purposes.

The type() function has two different forms:

type(name, bases, dict)

If a single object is passed to type(), the function returns its type:

>>> numbers_list = [1, 2, 3]
>>> print(type(numbers_list))
<class 'list'>
>>> class Lex:
a = 0
>>> lex = Lex()
>>> print(type(lex))
<class '__main__.Lex'>

If three parameters are passed to type(), it returns a new type object:

>>> o1 = type('X', (object,), dict(a='Foo', b=12))>>> print(type(o1))
<class 'type'>

Mutable Objects

Mutable objects are objects whose values ​​can be changed; Examples of mutable objects are lists, dictionaries, collections, and byte arrays.

For example:

#Create a list of elements
>>> list_1 = [1, 3.3, "lex"]
>>> print(id(list_1))

You can change some of these items over time, but the “id” of the list will remain the same, which means that the list will be the same object when the whole program is running, unless it is removed first, of course . We will replace one element of the list

>>> list_1[0] = 2
>>> list_1
[2, 3.3, "lex"]
>>> print(id(list_1))

What happens if we create two lists with the same value?

>>> n = [5, 4, 8]
>>> m = [5, 4, 8]
>>> n == m
>>> n is m

It’s because they refer to different objects.

If we want that both list refer to the same object we have to apply a technique called “aliasing”

>>> n = [5, 4, 8]
>>> m = n
>>> n is m

Immutable objects

An immutable object is an object that cannot be changed or edited after it is created. Examples are number, string, tuple, frozen set, and bytes.

>>> string_1 = "lex"
>>> string_1[0] = "p"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

Now, what happens if we assign the same value to two objects

>>> string_1 = "lex"
>>> string_2 = "lex"
>>> string_1 == string_2
>>> string_1 is string_2

That is because the refer to the same object, they have the same id.

>>> id(string_1), id(string_2)
(122843445625244, 122843445625244)# Both strings have the same id

Why does it matter and how differently does Python treat mutable and immutable objects

Below you can see a diagram that classifies each type in Python as mutable or immutable, and the relationship between these types and the variables assigned to them:

So, we can conclude that in mutable objects you can manipulate the value of this object to change its value, But vice versa, you cannot change the value of an immutable object.

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

For us, it is important to understand the difference between mutable and immutable types, and how to handle them when passing them to functions. When using appropriate objects, memory efficiency will be greatly reduced. For example, if a mutable object is called by reference in a function, the original variable itself can be changed. Therefore, to avoid this situation, the original variable must be copied to another variable. Immutable objects can be called by reference, because their value cannot be changed anyway.

>>> def updateList(list1):
... list1 += [3]
>>> n = [1, 2]
>>> print(id(n))
>>> updateList(n)
>>> print(n)
[1, 2, 3]
>>> print(id(n))

As we can see from the above example, we have called the list via call by reference, so the changes are made to the original list itself.

>>> def updateNumber(n):
... print(id(n))
... n += 10
>>> b = 5
>>> print(id(b))
>>>> updateNumber(b)
>>> print(b)

In the above example, the same object is passed to the function, but even if the object is the same, the value of the variable will not change. This is called pass by value. So, what is going on here? When a function calls a value, only the value of the variable is passed, not the object itself. Therefore, the variable that references the object will not change, but the object itself will change, but only within the scope of the function. Therefore, the changes were not reflected.

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:

/* References to small integers are saved in this array so that they
can be shared.
The integers that are saved are those in the range
-NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Py_ssize_t quick_int_allocs;
Py_ssize_t quick_neg_int_allocs;

What does this mean? This means that when you create an int from the range of -5 and 256, you are actually referencing to the existing object.




Bachelor Industrial Engineering

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to calculate exact cost and time for your next UX/UI freelance project

1̵4̵ 21(extra) Days in ATL: COVID-19

How to use an Api for rails project

Automate Your Flask Deployment to VPS Using Github Actions

DevOps / SRE — Top Links Last Week

What Are ERP? 6 Lesser-Known Benefits Of ERP

My First Experience With Competitive Coding In The February Hack the Interview with HackerRank

Road TO Dev — Boss Phase One movement

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alex Rivera Cruz

Alex Rivera Cruz

Bachelor Industrial Engineering

More from Medium

Check data type of variable in python

Measuring a lensless camera PSF and raw data

Bernoulli’s Principle & Bernoulli’s Equation — NEET Physics| Class 11