Andrés Felipe García Rendón
6 min readJan 15, 2020

--

Python3: Mutable, Immutable… everything is object!

Introduction

In this publication we will explain what are the differences between mutable objects and immutable objects in the Python programming language. Prior to this we will define in a clear, precise and simple way that it is a class, object, method, tuple and list.
Class: Classes provide a way to package data and functionality together. When creating a new class, a new type of object is created, allowing new instances of that type to be created.
Object: Everything in Python is an object, and almost everything has attributes and methods. All functions have a __doc__ attribute that returns the documentation string defined in their source code. The sys module is an object that contains (among other things) an attribute called path
Method: A method is a function that “belongs to” an object. In Python, the term method is not limited to class instances: other types of objects may also have methods.
Tuple: In Python, a tuple is an ordered and immutable set of elements of the same or different type.
List: it is a data structure and a type of data in python with special characteristics. The special thing about Python lists is that they allow us to store any type of value as integers, strings and even other functions; for example:
list = [1, 2.5, ‘DevCode’, [5,6], 4]

Id and type

id() is an inbuilt function in Python.
Syntax:

As we can see the function accepts a single parameter and is used to return the identity of an object. This identity has to be unique and constant for this object during the lifetime. Two objects with non-overlapping lifetimes may have the same id() value. If we relate this to C, then they are actually the memory address, here in Python it is the unique id. This function is generally used internally in Python.

Examples:

type() is a function in python.

type() method returns class type of the argument(object) passed as parameter. type() function is mostly used for debugging purposes.

Two different types of arguments can be passed to type() function, single and three argument. If single argument type(obj) is passed, it returns the type of given object. If three arguments type(name, bases, dict) is passed, it returns a new type object.

Syntax :

Example:

Python: Mutable vs. Immutable

Everything in Python is an object . You have to understand that Python represents all its data as objects. An object’s mutability is determined by its type. Some of these objects like lists and dictionaries are mutable , meaning you can change their content without changing their identity. Other objects like integers, floats, strings and tuples are objects that can not be changed.

Strings are Immutable

Strings are immutable in Python, which means you cannot change an existing string. The best you can do is create a new string that is a variation on the original.

example

Why are Python strings immutable?

Which means a string value cannot be updated . Immutability is a clean and efficient solution to concurrent access. Having immutable variables means that no matter how many times the method is called with the same variable/value, the output will always be the same. Having mutable variables means that calling the same method with the same variables may not guarantee the same output, because the variable can be mutated at any time by another method or perhaps, another thread, and that is where you start to go crazy debugging.

Example of list is mutable

Tupl:e is immutable

Immutable example

If you want to write most efficient code, you should be the knowing difference between mutable and immutable in python. Concatenating string in loops wastes lots of memory , because strings are immutable, concatenating two strings together actually creates a third string which is the combination of the previous two. If you are iterating a lot and building a large string, you will waste a lot of memory creating and throwing away objects. Use list compression join technique.

Python handles mutable and immutable objects differently. Immutable are quicker to access than mutable objects. Also, immutable objects are fundamentally expensive to “change”, because doing so involves creating a copy. Changing mutable objects is cheap.

How arguments are passed to functions?

The authors who call the mechanism call-by-value and those who call it call-by-reference are stretching the definitions until they fit.
Correctly speaking, Python uses a mechanism, which is known as “Call-by-Object”, sometimes also called “Call by Object Reference” or “Call by Sharing”.

If you pass immutable arguments like integers, strings or tuples to a function, the passing acts like call-by-value. The object reference is passed to the function parameters. They can’t be changed within the function, because they can’t be changed at all, i.e. they are immutable. It’s different, if we pass mutable arguments. They are also passed by object reference, but they can be changed in place in the function. If we pass a list to a function, we have to consider two cases: Elements of a list can be changed in place, i.e. the list will be changed even in the caller’s scope. If a new list is assigned to the name, the old list will not be affected, i.e. the list in the caller’s scope will remain untouched.

NSMALLPOSINTS, NSMALLNEGINTS

It turns out Python keeps an array of integer objects for “all integers between -5 and 256”. When we create an int in that range, we’re actually just getting a reference to the existing object in memory.

If we set x = 42, we are actually performing a search in the integer block for the value in the range -5 to +257. Once x falls out of the scope of this range, it will be garbage collected (destroyed) and be an entirely different object. The process of creating a new integer object and then destroying it immediately creates a lot of useless calculation cycles, so Python preallocated a range of commonly used integers.

--

--

Andrés Felipe García Rendón

Web Developer graduated from Holberton School. With experience in as C, Python, NodeJS, React, React Native, HTML, CSS, Boostrap and Flexbox