Python3: Mutable, Immutable… everything is object!

Object Oriented Programming

Object Oriented Programming (OOP) is a programming paradigm in which the relevant real world concepts for solving a problem are modeled through classes and objects; and under this concept, the programs consist of a series of interactions between these objects.

Object

To understand this paradigm we first have to understand what is a class and what is an object. An object is an entity that groups together a related state and functionality. The state of the object is defined through variables called attributes, while the functionality is modeled through functions that are known by the name of object methods.

Class

A class, on the other hand, is nothing more than a generic template from which to instantiate objects; template that is the one that defines what attributes and methods will have the objects of that class. This is why everything in Python is an object.

>>> isinstance(1, object)
True
>>> isinstance(False, object)
True
def my_func():
return "hello"
>>> isinstance(my_func, object)
True
Standard compound data types
  • Immutable Objects: because their content (or that value) cannot be changed at runtime.

Type

The simplest way to check the type of object we are working with in Python is to use the built-in type() function. This will allow us to see that everything can be treated in the same way, as an object -or instance- of the class to which they belong.

>>> x = 42 
>>> typex
<class 'int'>
>>> y = 24.5
>>> type(y)
<class 'float'>
>>> def f(x):
... return (x+1)
...
>>> type(f)
<class 'function'>
>>> import math
>>> type(math)
<class 'module'>

Identity

Another of the built-in Python functions is id() which returns the address of an object in memory.

>>> x = 1
>>> id(x)
10105088
>>> x = "Holberton"
>>> y = "Holberton"
>>> x == y
True
>>> x = "Holberton School"
>>> y = "Holberton School"
>>> x == y
True
>>> id(x)
139798528064800
>>> id(y)
139798528064872
>>> id(x) == id(y)
False
>>> x == y
True
>>> id(x) == id(y)
False
>>> x is y
False
>>> x = "Holberton School"
>>> z = x
>>> id(x)
139798528064944
>>> id(z)
139798528064944
>>> x == z
True

Mutable and Immutable Objects

As we indicated, in Python everything is an object, however, there is an important distinction between objects. Some objects are mutable while others are immutable.

Immutable objects

For some types in Python, once we have instantiated those types, they never change. They are immutable. For example, int objects are immutable in Python. What will happen if we try to change the value of an int object?

>>> x = 37598
>>> x
37598
>>> x = 37599
>>> x
37599
>>> x = 37598
>>> x
37598
>>> id(x)
139798529290128
>>> x = 37599
>>> x
37599
>>> id(x)
139798528694160
>>> my_tuple = (1, 2, 3)
>>> id(my_tuple)
139798528063096
>>> my_tuple = (3, 4, 5)
>>> id(my_tuple)
139798528064608
>>> my_tuple[0] = 'a new value'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

Mutable Objects

Some types in Python can be modified after creation and are called mutables. For example, we know that we can modify the content of a list object:

>>> my_list = [1, 2, 3]
>>> my_list[0] = 'new value'
>>> my_list
['new value', 2, 3]
>>> my_list = [1, 2, 3]
>>> id(my_list)
139798488481416
>>> my_list
[1, 2, 3]
>>> my_list[0] = 'new value'
>>> id(my_list)
139798488481416
>>> my_list
['new value', 2, 3]
>>> x = y = [1, 2]
>>> x is y
True
>>> id(x)
139798488480520
>>> id(y)
139798488480520
>>> id(x) == id(y)
True
>>> x.append(3)
>>> x
[1, 2, 3]
>>> y
[1, 2, 3]
>>> id(x)
139798488480520
>>> id(y)
139798488480520

Why is it important and how does Python handle mutable and immutable objects?

It is important to know how Python handles mutable and immutable objects to avoid errors or modifying data when that is not the wish. Let’s look at an example.

>>> my_list = [(1, 1), 2, 3]
>>> my_tuple = ([1, 1], 2, 3)
>>> type(my_list)
<class 'list'>
>>> type(my_list[0])
<class 'tuple'>
>>> type(my_tuple)
<class 'tuple'>
>>> type(my_tuple[0])
<class 'list'>
>>> my_list[0][0] = 'Changed!'    (1)
>>> my_tuple[0][0] = 'Changed!' (2)
>>> my_list[0][0] = 'Changed!'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> my_tuple = ([1, 1], 2, 3)
>>> id(my_tuple)
139798528063096
>>> type(my_tuple[0])
<class 'list'>
>>> id(my_tuple[0])
139798520723336
>>> my_tuple[0][0] = 'Changed!'
>>> id(my_tuple)
139798528063096
>>> id(my_tuple[0])
139798520723336
>>> my_tuple
(['Changed!', 1], 2, 3)

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

Its important to know the difference between mutable and immutable types and how they are treated when passed onto functions. Memory efficiency is highly affected when the proper objects are used.

>>> def increment(n):
... n += 1
...
>>> a = 9
>>> increment(a)
>>> a
9

>>> def increment(l):
... l += [4]
...
>>> l = [1, 2, 3]
>>> increment(l)
>>> l
[1, 2, 3, 4]

Preallocation in Python

Now a homework for you:
1. create two variables with values between -5 and 256 and then check if they reference to the same object.
2. Do the same but using values for the variables out of the range above.
What happened?

--

--

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