Understanding Objects and Mutability in Python
The mythical man month - Fred Brooks

Understanding Objects and Mutability in Python

In the previous blog it was briefly discussed the concepts of objects, classes and instance attributes in python under OOP (Object Oriented Programming) so it is recommended to read the previous blog so you can have a better understanding of objects and mutability between them.

Overview

Object Oriented Programming is a programming language model in which programs are organized around data, objects rather than functions and logic. This in other words means that the software design process is by applying data structures. However, the data structure in the OOP becomes and object that includes data and functions creating relationships between objects thus the objects can inherit characteristics from other objects.

Python is known for being an OOP langugae meaning that its framework of programming is focused on objects, allowing the user to create and manipulte atributes and methods so the program can be consistent and efficient by having the required structures. One of Python's core is based from the PyObject data structure and the data types are inherited from PyObjects.

Then, as you saw Python is oriented to objects and the relations between them, so the objects and the structure need to have an identity so the user can interact with them and modify them.

Basics on identity and type

Every object has an identity and a type. the identity of an object never is going to change once is created, it is unique and constant. The identity of an object in Python is like the address in memory and by using the is operator the user can compare the identity of two objects an using id( ) the return is the integer that represents the identity. (see image 1).

Image 1

Note that obj1 and obj2 when using is the return is False the reason is because the identity is different despite having the same type, this also can be validated when the id of the objects presents a different address. But what happens when obj1 and obj2 contains the same value? This is called "Alias" and means that the identity is going to be the same and both objects are referring to the same value. So the user changes the value of a function argument inside the function code block is what is call assignment.

Image 2

As for the type, this function means that the object’s type defines the possible values and operations that type supports. The type() function returns the type of an object. An object type is unchangeable like the identity. An important thing to remember is that the value of some objects can change. Objects whose value can change are said to be mutable; otherwise objects whose value is unchangeable once they are created are called immutable.

The user has to be aware that some objects contain references to other objects. These objects are called containers and in Python the example of container are a tuple, list, and dictionary. Understanding this will help for a better comprehension of what a mutable and immutable objects are.

Mutability vs Immutability

Before getting into the functionality of the mutability and the immutability in Python, it is importan to understand that the objects whose value can change are mutable and objects whose value is unchangeable once they are created are called immutable.

Immutable objects

Immutable objects are types int, float, decimal, bool, string, tuple, and range. For example lets take a list and a tuple and print the elements at the [0] position. the output in the example is 1 and 3 respectively (see image 3). Now, if the user wants to add values to the list and the tuple, the list values can be added due its nature as mutable object, but the tuple cannot be modified. see image 4 when a error is raised for the tuple.

Image 3
Image 4

Another example that can provide insights about the behavior of the immutable objects is this. Let's say that the user has a tuple of elements (1, 2, 3) and he wants to expand it by adding 3 new elements (4, 5, 6); the image 5 shows that adding the elements doesn't change the id of the tuple.

Image 5

This example can give the insight that the tuple was expanded but it created a new tuple due its new identity. Things are different for example with a list. The id of the expansion of the list does not change despite there are new elements at the list.

No alt text provided for this image

Mutable objects

Mutable objects are objects whose values can change. Examples of mutable types in Python are list, dictionary, set and user-defined classes. In the image above it can be shown the behavior of a mutable object in a list. However, the best way to understand the mutability of objects is when you are concatenating strings. The image below shows that when the user creates two strings the id of each object changes. Then each time a new string is created it becomes an object and when is concatenated the object value and so the id changes too

No alt text provided for this image

Bonus: Pre-allocation in Python

Notice that Pyhton it's a language that only describes or states how the user express to the interpreter (the program which accepts your python code) Python language has been implemented in various other languages, so when someone says they use Python, it is most probably CPython, the original and the reference Python implementation. CPython is the reference implementation of Python written in the C programming language, but CPython is the most widely used and the one against all others are compared.

Then, indeed Python works with a pre-allocation concept due CPython keeps an array of integer objects for all integers between -5 and 256. When the user creates an int in that range its getting back reference to the existing object decided that that's a good range to have pre-allocated; that's for performance reasons and it covers the most commonly used integer values. CPython documentation states that:

/* Small integers are preallocated in this array so that they
   can be shared.
   The integers that are preallocated are those in the range
   -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
*/

So, the range of integers from -5 (inclusive) to 257 (non-inclusive) are macros defined as NSMALLPOSINTS and NSMALLNEGINTS.

#define NSMALLPOSINTS           257
#define NSMALLNEGINTS           -5

The importance to understand this is that in Python the user may seem that sometimes an immutable object such the tuple can be mutable and that happens for the pre-allocation and because these objects are always reused to optimize the memory in Python implementation of CPython

Synopsis

In Python every object has an identity, type an a value; the objects whose value can change are said to be mutable and those whose value is unchangeable once they are created are called immutable. The object Id never changes once it has been created. Think of it as the object’s address in memory. Python has a characteristic and is that OOP code is represented by objects or by relations between objects. However be aware that the object’s type defines the possible values and operations.

Interesting Fact

The image above is the cover of the book "The mythical man month" considered as the "Bible of Software Engineering". This subject of mutability in python, handles a lot of the framework when designing software and project management refers. So if you are interesting in knowing more about the software engineering frameworks but from the core of everything this book will give you a big picture of how to handle programming systems

To view or add a comment, sign in

More articles by Edward A Ortiz

Explore content categories