Mutable, Immutable… everything is object!

Redondo--Tanis Melvin
4 min readDec 9, 2024

--

A dog makes OOP

In Python, key phrases like “strings”, “lists”, and “dictionaries” may seem straightforward, but beneath these simple names lies a complex nature. In fact, every object in Python has a fundamental property that sets it apart: everything is an object. And it’s this very essence that gives rise to the rules of immutability and mutability. In this article, we’ll delve into this core concept to gain a deeper understanding of how Python works.

Before diving into specific concepts, it’s essential to understand the key terms used in Programming Oriented Object (POO) in Python.

  • Object: An entity that represents something in the real or virtual world.
  • Class: A template that defines an object’s structure and behavior.
  • Instance: A copy of a class, i.e., an individual object created from a class.
  • Attribute: A property or variable associated with an object, used to store data.
  • Method: An attribute-specific feature of an object, called when the attribute is invoked.
  • Inheritance: The process by which an object can inherit properties and behaviors from another object (or class).
  • Polymorphism: The ability of an object to take different forms or behave differently based on context.
  • Encapsulation: The notion that a hidden internal property is controlled and protected by a method, protecting the program’s integrity.
  • Alias: A nickname or synonym for a term.

Id and Type

In Python, everything is an object. Simply put, this means that every variable in your code is an instance of its type.

Id
An ID (Identifier) is a unique, global value assigned to each object created. It is a character string that specifically identifies an object in the system.

The ID is used to store the object’s memory location in the Python memory management stack. This enables efficient search and retrieval of created objects.

>>> x = 666
>>> id(x)
125476223572392

Type
A type is a category that describes the nature of an object. Types are used to determine the operations that can be performed on an object.

>>> x = 666
>>> type(x)
<class 'int'>

Variables with the same value are considered equal and have the advantage of being stored in the same memory location, which means they share the same ID.

>>> x = 5
>>> y = x
>>> id(x)
>>> id(y)
125476223572392
125476223572392

Mutable and Immutable

There are two types of variable: mutable and immutable.

Mutable variables
Mutable variables are objects that can be modified after they have been created. When you modify a mutable variable, you are actually modifying the object itself.

>>> x = [1, 2, 3]
>>> x.append(4)
>>> print(x)
[1, 2, 3, 4]

>>> y = x
>>> y.append(5)
>>> print(y)
[1, 2, 3, 4, 5]

Immutable variables
Immutable variables are objects that cannot be modified once they have been created. When you try to modify an immutable variable, you obtain a new instance of the object.

>>> x = (1, 2, 3)
>>> y = x
>>> y[0] = 10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

Transferring arguments to functions

When you call a function, the arguments you pass to the function are passed as references. This means that the values of the calling variables are not copied into the function’s environment, but rather references to the original variables are passed.

There are two types of argument transfer:

  1. call by value: In this case, the values of the calling variables are copied and passed on to the function. Changes made to variables in the function have no effect on the original variables.
  2. call by reference: In this case, references to call variables are passed to the function. Changes made to variables in the function have a direct effect on the original variables.

Mutable objects
When you pass a mutable variable as an argument to a function, the function receives a reference to the original object. This means that if the function modifies the object, it also affects the original call variable.

>>> def modify_list(lst, nb):
... lst[0] = nb
...
>>> my_list = [1, 2, 3]
>>> modify_list(my_list, 10)
>>> print(my_list)
[10, 2, 3]

In this example, the modify_list function receives a reference to the list my_list. Modifying the first element of the list also affects the original call variable.

Immutable objects
When you pass an immutable variable as an argument to a function, the function receives a copy of the object. This means that if the function modifies the copy, it will not affect the original calling variable.

>>> def modify_tuple(tup, nb):
... tup[0] = nb
...
>>> my_tuple = (1, 2, 3)
>>> modify_tuple(my_tuple)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in modify_tuple
TypeError: 'tuple' object does not support item assignment

In this example, the function modify_tuple receives a copy of the tuple my_tuple. Modifying the first element of the tuple also affects the original call variable. As the element is immutable, this causes an error.

--

--

Redondo--Tanis Melvin
Redondo--Tanis Melvin

Written by Redondo--Tanis Melvin

0 Followers

🌟 Hello, I'm a young developer passionate about exploring the depths of computer science through insightful articles.

No responses yet