Mutable vs Immutable Objects in Python

In Python every instance of a variable is considered an object, besides that we have two types of objects in Python that are mutable and immutable, in order to understand the most typical errors when using them we must understand how they work internally in Python.

To make it easier for you we are going to teach you 5 basic concepts so you can understand how they work:

  1. Immutable objects have read-only value

Examples Numbers, Boolean, Integer, Float, Strings, Tuples

2. Names (attrs.) are pointers and = copies the reference

3. Mutable objects provide in place modifiers

Examples list.append(), dict.update(), set.add()

4. Python code in interpreted at import time.

5. Interpreted means executed This include objects creation.

Id and Type

Id is a built in functions in python.

This function accepts a unique parameter and is used to return the id of the object, this identity is unique and does not change during the life of the object, as we have said two objects in python will never have the same id.

In fact what happens below is similar to what happens in the C language that what it does is to point to the memory address of the object, this is done internally in python.


Type() is a method that gives us back the kind of object class we’ve passed as a parameter.

Type() is mainly used to perform debugging functions, it can be passed two different types of arguments, which can be a single simple argument (obj) and three type arguments (name, bases, dict), the first one returns the type of the object we have passed as a parameter and the second one returns a new type of object.

Applications :

type() is mostly used for code debugging functions.

type() can be used for at that point to determine type text extracted and change it to other form of string.

type() can be used with three arguments dynamically initialize classes or existing classes with attributes.

There are two types of objects in Python. Immutable types and mutable types.


An object of an immutable type cannot be changed. Any attempt to change the object will result in a copy being created.

This category includes: integers, floats, complexes, strings, bytes, tuples, ranges and image sets.

To highlight this property, let’s play with the built-in id. This function returns the unique identifier of the object passed as a parameter. If the id is the same, this is the same object. If it changes, then this is another object. (Some say that this is actually the memory address of the object, but beware of them, they are from the dark side of the force…)


An object of a mutable type can be changed and is changed in situ . No implicit copies are made.

This category includes: lists, dictionaries, byte arrays and sets.

Let’s keep playing with our little id function.

What have we got? We create a byte array, we modify it and using the id , we can make sure that this is the same object, modified. It’s not a copy of that.

Of course, if an object is going to be modified frequently, a mutable type does a much better job than an immutable type.


To conclude we have tried to explain in this post in the most simple way and that you can understand better how python works internally to understand better how we can work with objects in python. Thanks for your time and we hope to see you soon with another topic that is of your liking.

Until the next post…





Software Enginner Student at @HolbertonCOL, Frontend Developer, Entusiast, Geek Life.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Black Box Testing: Types and Associated Techniques

ImpactQA — Black Box Testing: Types & Associated Techniques

Preview of IPX Wallet

5 years of Learning to be a Software Engineer ( My Experience and What I learnt )

The 9 Most Common Project Management Mistakes

Writing a Linux Kernel Module

Setting Up MongoDB

MongoDB logo

InvArch Ambassador Program

Flutter: Uploading Image to Server

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
WR Web Developer

WR Web Developer

Software Enginner Student at @HolbertonCOL, Frontend Developer, Entusiast, Geek Life.

More from Medium

Pytest- A programmer's friend

Python — List comprehension

Why you should use Catch-All Unpacking and not Slicing in Python