Python3: Mutable, Immutable… everything is object!

everything is an object :)))

Python is an object oriented programming language.

Almost everything in Python is an object, with its properties and methods.

A Class is like an object constructor and it used to create object !

in this article we will see together some new things about object in python :

  • id and type
  • mutable objects
  • immutable objects
  • why does it matter and how differently does Python treat mutable and immutable objects
  • how arguments are passed to functions and what does that imply for mutable and immutable objects

let’s strat step by step with :

ID’s and TYPES :

evry thing in our computer is stored somewhere in main memory , and to know for example and id of an object in Python , follow this example we will llok at an id of a ‘x’ variable:

we use the function id()

the id of object “x” is 9755296.

lets pass to the types

evry object have a type and to know our object type

we use function type() and here’s a clear example :

we get every object type by just using type() func ;) .

another thing we need to know about it , that python can refer to the same object by more than one variable , and this is for saving resources python devloppers make the possibility of referring the same object value by more than one variable , this called mutable object look at this example :

>>> a = 1
>>> b = a
>>> a = 2
>>> b
1
>>>

and this one also :

>>> a = [1, 2, 3]
>>> b = a
>>> a[0] = 'x'
>>> b
['x', 2, 3]
>>>

so we find here mutable object (that can be modified) without knowing his value !! such as lists , dictionary, set …

so wich object we can change them and wich are imutable ?

there are other type of object can’t be affected by changes in python

such as (int, float, complex, string, tuple, frozen set , bytes).

aymen@ubuntu:~$ python3
>>> holberton = (1, 2, 3, 9)
>>> print(holberton)
(1, 2, 3, 9)
>>> holberton[1] = 99
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>>

here i create a tuple called “holberton” and when i try to change his first digit ( number 2) by changing it to 99 , intrepreter raise a TypeError that tuple is immutable !

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

Python handles mutable and immutable objects differently. Immutable are quicker to access than mutable objects. Mutable objects are great to use when you need to change the size of the object, example list, dict etc.. Immutables are used when you need to ensure that the object you made will always stay the same.

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

Arguments are always passed to functions by reference in Python. The caller and the function code blocks share the same object or variable. When we change the value of a function argument inside the function code block scope, the value of that variable also changes inside the caller code block scope regardless of the name of the argument or variable. This concept behaves differently for both mutable and immutable arguments in Python.

In Python, integer, float, string and tuple are immutable objects. list, dict and set fall in the mutable object category. This means the value of integer, float, string or tuple is not changed in the calling block if their value is changed inside the function or method block but the value of list, dict or set object is changed. Consider the mutable and immutable as states of the function arguments in Python.

Python Immutable Function Arguments

Python immutable objects, such as numbers, tuple and strings, are also passed by reference like mutable objects, such as list, set and dict. Due to state of immutable (unchangeable) objects if an integer or string value is changed inside the function block then it much behaves like an object copying. A local new duplicate copy of the caller (main )object inside the function block scope is created and manipulated. The caller object will remain unchanged. Therefore, caller block will not notice any changes made inside the function block scope to the immutable object. Let’s take a look at the following example.

aymen@ubuntu:~$ python3
>>> x = (1, 2, 3)
>>> type(x)
<class 'tuple'>
>>> id(x)
139846727805312
>>> i = x
>>> id(i)
139846727805312
>>> i[0] = 4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> print(i)
(1, 2, 3)
>>> print(x)
(1, 2, 3)
>>>

in this example we created “x” and “i” variable pointing to the same tuple (id)

when i try to change vlaue of first digit in tuple “i” i couldn't because its immutable but we get a copy of it !

Junior Blockchain developer, passionate about P2P Decentralized Networks and System programming & Algorithms.