Everything is object

ahmed belhassen
6 min readMay 25, 2021

Introduction

To start from the very beginning we need to understand what is

  • What is object-oriented programming?

Object-Oriented Programing It is a programming paradigm (which is a basic model of the project and program design) that is based on 4 principal pillars which are abstraction, encapsulation, inheritance, and polymorphism. That helps us to abstract objects from the real world.

  • What is a Class?

A class is the prototype or code template that indicates what characteristics they will have and how the elements(objects) created from that class will behave. A class is a formal description of how an object is designed.

  • What is an object?

Objects are instances of the class. An object or instance is actual encapsulated data using the attributes and methods of its class, it lives in memory as the process exists or until destroyed.

Even though everything is an object in Python, not all the objects handle changes in the same way, some of them are mutable and other immutable objects.

When an object is initiated, it is assigned a unique object id. Its type is defined at runtime and once it’s set it can never change, however its state can be changed if object is mutable. A mutable object can be changed after it is created, and an immutable object cannot.

Let’s dive deeper in the Mutability and Immutability of an object.

Mutable and Immutable Objects

Mutable objects

In Python, some common mutable objects include lists, dicts, and sets. In simple terms, when an object is referred to as being mutable, it means that the object, or the contents of that object can be altered.

Immutable objects

Common immutable objects in Python include ints, floats, tuples, and strings. Unlike mutable objects, the immutable objects can’t be changed.

Difference Between Mutable and Immutable objects

# Mutables
my_list = [10, 20, 30]
print(my_list)
# [10, 20, 30]my_list = [10, 20, 30]
my_list[0] = 40
print(my_list)
# [40, 20, 30]# Immutable
my_yuple = (10, 20, 30)
print(my_yuple)
# (10, 20, 30)
my_yuple = (10, 20, 30)
my_yuple[0] = 40
print(my_yuple)
# Traceback (most recent call last):
File "test.py", line 3, in < module >
my_yuple[0] = 40
TypeError: 'tuple' object does not support item assignment

Python offers some tools to find out whether an object is mutable or immutable through, the functions id( ) and type( ).

Id and type

  • Id function: Type( ) it’s the function we use to print the type of an object.
>>> a = 89
>>> b = a
>>> id(a)
10107904
>>> id(b)
10107904
>>> a is b
True
  • Type function: with id( ) fucntion we can get the variable identifier (which is the memory address in the CPython implementation).
>>> a = 89
>>> b = a
>>> type(a)
<class 'int'>
>>> type(b)
<class 'int'>

Objects and its relation with mutable or inmutable types

To Understand this relationship let’s take a closer look to the following example:

a = "banana"
b = "banana"

we have two variables where a and b will refer to a string with the letters “banana”. But we don’t know yet whether they point to the same string.

There are two possible states

In one case, a and b refer to two different objects that have the same value. In the second case, they refer to the same object. An object is something a variable can refer to.

We can test whether two names have the same value using “==”:

>>> a = "banana"
>>> b = "banana"
>>> a == b
True

We can test whether two names refer to the same object using the “is” operator:

>>> a = "banana"
>>> b = "banana"
>>> a is b
True

This tells us that both a and b refer to the same object, and that is the diagram above the one that describes the relationship. Since strings are immutable, Python optimizes resources by making two names that refer to the same string value refer to the same object.

On the other hand we have a different approach with lists:

>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a == b
True
>>> a is b
False

The relationship between a and b is the following:

In this scenario a and b have the same value but do not refer to the same object.

On the other hand, if we assign one variable to another, both variables refer to the same object and the same list has two different names, so we create an Alias to the list, as follows:

>>> a = [1, 2, 3]
>>> b = a
>>> a is b
True

The relationship between a and b is the following:

Because the same list has two different names, a and b, we say that it is aliased. Changes made with one alias affect the other:

>>> a = [1, 2, 3]
>>> b = ae
>>> b[0] = 5
>>> print(a)
[5, 2, 3]

Although this behavior can be useful, it is sometimes unexpected. In general, it is safer to avoid setting Alias when we are working with mutable objects. Of course, for immutable objects, there’s no problem. That’s why Python is free to alias strings when it sees an opportunity to economize.

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

Making objects immutable or mutable is important whather it matters to your program execution, for example if you have to make sure is not changed then you need to thing of a data type immutable, or otherwise. This will guarantee that an object remains constant throughout the program. And rember that a mutable object can be changed after it is created, and an immutable object cannot.

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

Although we now understand what exactly mutable and immutable objects are, we still need to examine how these objects behave when they are passed to functions. There are mainly two ways that objects get passed to functions. Passing by reference means that the called functions parameter will be the same as the caller’s passed argument. On the other hand, pass by value means the called function’s parameter will be a copy of the caller’s passed argument. First we’ll pass an immutable object to a function:

Tuples in Python are Immutables but potentially changing

Python tuples have a surprising trait: they are immutable, but their values may change. This may happen when a tuple holds a reference to any mutable object, such as a list.

--

--