Angel Palma
4 min readMay 28, 2020

Python3: Mutable, Immutable… everything is object!

Python is an Object Oriented Programming (OOP) language, but what is object oriented programming?

The OOP is a programming paradigm, a way to program and organize our code in classes from which objects that are related to each other are created to achieve the objectives of the applications. Is a special way to program that is closer than the way that we would express things in real life.

Classes

Classes are models on which our objects will be built. In python we define a class with the class <NameClass> instruction, like this:

class Antenna:
pass
class Hair:
pass
class Eye:
pass
class Object:
pass

Properties

Properties are the characteristics of the object, they are represented like variables, but technically their are named properties.

class Antenna:
color = ""
length = ""
class Hair:
color = ""
texture = ""
class Eye:
color = ""
size = ""
shape = ""
class Object:
color = ""
size = ""
appearance = ""
antenna = Antenna()
eye = Eye()
hair = Hair()

Methods

Methods are functions but technically called methods. Methods are the proper actions than a specific object can do.

class Antenna:
color = ""
length = ""
class Hair:
color = ""
texture = ""
class Eye:
color = ""
size = ""
shape = ""
class Object:
color = "green"
size = "big"
appearance = "ugly"
antenna = Antenna()
eye = Eye()
hair = Hair()

def float(self):
pass

In the above example we can see a float function that is defined into the class Object.

Object

Classes are methods to create objects, so we can say Objects are the materialization of a class reasoning.

class Antenna:
color = ""
length = ""
class Hair:
color = ""
texture = ""
class Eye:
color = ""
size = ""
shape = ""
class Object:
color = "green"
size = "big"
appearance = "ugly"
antenna = Antenna()
eye = Eye()
hair = Hair()

def float(self):
print(12)
a = Object()
print(a.color)
print(a.size)
print(a.appearance)
a.color = "red"
print(a.color)
OUTPUT:
green
big
ugly
red

id()

In Python id() is a built in function that accepts a single parameter and returns the identity of an object. During the object lifetime this identity has to be unique.

>>> a = 5
>>> b = 5
>>> id(5)
9756320
>>> id(a)
9756320
>>> id(b)
9756320

In the above example we created two variables (a b) and assigned the 5 int value to each one. Then we used the id() function to see the id of each one. We can see that the output of id(5), id(a) and id(b) are equals. What is the meaning of that?

In Python everything is object, and the number 5 is an object, we assigned the memory location of that object to a variable a, then in the second line code we assigned the value 5 to variable b, but Python already knows that an object 5 exist, so Python don’t create a new object 5, it used the existing object and points the variable b to that object, for that reason all the id outputs are equals.

Type()

Type() method returns class type of the argument(object).

>>> a = 5
>>> b = 5
>>> id(5)
9756320
>>> id(a)
9756320
>>> id(b)
9756320
>>> type(a)
<class 'int'>
>>> a = 5.2
>>> type(a)
<class 'float'>

In the above example we can see that type(a) returns class ‘int’, that’s because a is equal to 5 that is an integer, then we equalize a to 5.2 and it returns a class ‘float’.

>>> a = (2, 3)
>>> type(a)
<class 'tuple'>
>>> a = (2)
>>> type(a)
<class 'int'>

Why if we are using a = (2) is not a tuple but a = (2, 3) is a tuple? Parenthesis in Python are just for group, the syntax of a tuple require minimum one number and one coma like this:

>>> a = (2, )
>>> type(a)
<class 'tuple'>

Mutable and Immutable Objects

There are two types of objects in Python Mutable and Immutable objects, Mutable objects refer to the objects that can change their state or contents and Immutable objects can’t change their state or content.

There are examples of Immutable Objects******************************************
>>> a = "Holberton School"
>>> a[2] = "L"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
******************************************
>>> tuple = (1, 2, 3)
>>> tuple[2] = 5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
******************************************

A Immutable Object can’t change their state after it is crated. These are of in-built types like int, float, bool, string, unicode, tuple.

There are examples of Mutable Objects******************************************
>>> color = ["yellow", "blue", "red"]
>>> color
['yellow', 'blue', 'red']
>>> type(color)
<class 'list'>
>>> color[0] = "black"
>>> color
['black', 'blue', 'red']
>>> type(color)
<class 'list'>
******************************************

Mutable Object can change after it is created, these are of type list, dict, set.

Use of mutable objects is recommended when we need to change the size or content of an object, we need to take with that because we can change important data and break our code. Take care of that and document every change. Immutable objects are quicker to access but difficult to change, because it involves creation of a copy.

Angel Palma
Angel Palma

Written by Angel Palma

Electronic engineer and software developer.

No responses yet