Sitemap
Marcela Sanchez Moreno

Marcela Sanchez Moreno

Software Engineering Student 💻 Enthusiastic of macramé and crochet 🧶 ❤

Python3: Mutable, Immutable… everything is an object!

3 min readJan 16, 2020
Press enter or click to view image in full size
https://realpython.com/

First of all, let’s give a little review of some basic concepts that are needed to understand why everything is an object in Python, such as object-oriented programming.

What is object-oriented programming?

Object-Oriented Programming (OOP) is a programming paradigm (set of theories, standards, models and methods that allow organizing knowledge) that uses objects and their interactions to design applications and computer programs. It is a particular way of programming because it uses the analogy of how things behave in real life, the main element to explain this are the objects, because it is a unit that contains the characteristics, attributes and behaviors in itself, which makes it independent, but by relating to objects of the same class, you can define and visualize problems and implement their solution through the programming language, in this case, Python 3.

Knowing this, what is an object?

An instance of a class. It is a set of properties or attributes (data) and behavior or functionality (methods), which consequently react to events. They correspond with the real objects of the world around us, or with internal objects of the system (of the program). It is an instance of a class.

These previous concepts are very important to understand this programming language, and more so for a person new to this field.

Now let’s go a little deeper into this.

In Python all objects can be mutable or immutable, some of the mutable objects in Python are: lists and dictionaries and some of the immutable objects are: int, float, tuples, and strings.

Get Marcela Sanchez Moreno’s stories in your inbox

Join Medium for free to get updates from this writer.

These objects are something you can refer to in the code, as we’ll see in the following examples, but before this, as we really know if an object is mutable or immutable, what tools does Python offer us to find out: through the functions “id” and “type”

  • “id” function:
Python 3 interactive mode

“id” is a built-in function in Python.
Syntax: id(object)
This function accepts a parameter and is used to return the identity of an object. This identity is unique and constant for the object that passes through the function, as in other programming languages (such as C) this can only be the memory address of the object.

When in python’s interactive mode we put “a is b”, we want to check if the variables refer to the same object.

  • “type” function:
Press enter or click to view image in full size
Python 3 interactive mode

“type” is a method that returns the class type of an object (argument) passed as a parameter, this means that it returns the type of argument within the function.
Syntax :
type(object)

In the previous image, you can see how the type of the variable “a” returns, which is an integer.

Up to this point, we have seen how we can compare two variables to find out through the functions “type” and “id”, their corresponding identification and type. With these, we can see how the variables are associated with the different types of objects and how in a hypothetical case, these could be changed by different values.

A mutable object can change its state or content since it is created and immutable objects cannot do so, they stay with the value they are initially assigned.

  1. .Mutable objects:
  2. Immutable objects:

--

--

Marcela Sanchez Moreno
Marcela Sanchez Moreno

Written by Marcela Sanchez Moreno

Software Engineering Student 💻 Enthusiastic of macramé and crochet 🧶 ❤