Python3: Mutable, Immutable… everything is object!

Bouzouitina Hamdi
4 min readMay 26, 2020

--

Everything in python is an object

Introduction

Everything in Python is an object. An object is an entity that contains data along with associated metadata and/or functionality. In Python everything is an object, which means every entity has some metadata (called attributes) and associated functionality (called methods).

Id and Type

The id() function returns identity of the object. This is an integer which is unique for the given object and remains constant during its lifetime and it takes a single parameter object.

The id()

The type() function either returns the type of the object or returns a new type object based on the arguments passed.

The type()

Mutable and Immutable Objects

Every variable in python holds an instance of an object. There are two types of objects in python i.e. Mutable and Immutable objects. Whenever an object is instantiated, it is assigned a unique object id. The type of the object is defined at the runtime and it can’t be changed afterwards. However, it’s state can be changed if it is a mutable object.

To summarise the difference, mutable objects can change their state or contents and immutable objects can’t change their state or content.

  • Immutable objects
Immutable objects

Immutable objects doesn’t allow modification after creation.

  • Mutable objects
Mutable objects

Object id will not be changed.m and n will be pointing to the same list object after the modification. The list object will now contain [1, 2, 3, 4].

How Python Treats Mutable and Immutable Objects

Mutable and immutable objects are handled differently in python. Immutable objects are quicker to access and are expensive to change because it involves the creation of a copy.

Use of mutable objects is recommended when there is a need to change the size or content of the object.

  • Exception: The tuple

The tuple consists of a string and a list. Strings are immutable so we can’t change its value. But the contents of the list can change. The tuple itself isn’t mutable but contain items that are mutable.

How Python Objects Get Passed

A mutable object is called by reference in a function, it can change the original variable itself. Hence to avoid this, the original variable needs to be copied to another variable.

A immutable object is called by pass by value in a function.

We will never be able to manipulate immutable objects by passing it to functions. But, we can still “modify” immutable objects by capturing the return of the function.

By assigning the return value of the function to “a”, we have reassigned a to refer to the new object with the value 2 created in the function. Note the object a initially referred to never change , it is still 1 but by having ”a”point to a new object created by the function.

  • Extra information : Use of __slots__

When we create objects for classes, it requires memory and the attribute are stored in the form of a dictionary. In case if we need to allocate thousands of objects, it will take a lot of memory space.
For every instance object, we will have an instance of a dictionary that consumes more space and wastes a lot of RAM. In Python, there is no default functionality to allocate a static amount of memory while creating the object to store all its attributes.
Usage of __slots__ reduce the wastage of space and speed up the program by allocating space for a fixed amount of attributes.

--

--

No responses yet