Python's Mutable vs Immutable Objects
Impact on Functions and Data

Python's Mutable vs Immutable Objects

Introduction

A fundamental topic in the programming language: mutable and immutable objects. To begin with, it is important to understand that everything in Python is an object. Each object has an identifier and a type, which are values that can be obtained using the id() and type() functions.


What is id() and type()?

The python documentation says that "the id() function returns an integer representing its identity", what is understood is that id() returns a unique identifier for an object, as if it were its ID number. On the other hand, the documentation also states that "the type() function returns the type of an object (which is itself an object)". These functions are very useful because they allow us to obtain information about an object in Python.


Mutable object

When we say that an object is mutable we mean that it has the ability to change its value or content after it has been created. The memory storage is done by means of the memory address of a mutable object in the variable.

Immutable object

Unlike mutable objects, immutable objects cannot be modified, although it may happen that an immutable object contains a mutable object. Immutable objects are stored directly in the memory allocated to the variable that references them. When the variable is accessed, Python searches memory for the memory address it points to, accesses the object at that address, and returns its value.


No hay texto alternativo para esta imagen

Understanding the difference between mutable and immutable objects is fundamental to programming. This can affect how arguments are passed to functions and how objects are handled within them. Therefore, it is critical that we keep these differences in mind when writing our code.


Assignment and referencing in python

These are closely related concepts.

An example to understand assignment, is when a variable is created and a value is given or assigned to this variable.

num = 1        

The variable num points to the value 1.

The reference is understood as a tag that refers to an object that already exists.

list = ["banana", "watermelon", "lemon"]
fruits = list        

In the previous example we have a list containing the names of some fruits, and we make a reference to the fruits variable to point to this same list. In this case we are not creating a new list, we are creating two different tags (list, fruits) that point to the same object.


How arguments are passed to functions and what does this imply for mutable and immutable objects?

Arguments are passed to functions by value, but in the case of mutable objects, they are passed by reference to the object. This means that, when a function is called with an immutable object, a copy of the object's value is created at a new memory address and passed to the function, whereas, in the case of mutable objects, a reference to the original object is passed.

When immutable objects are passed as arguments to a function, they cannot be modified within the function. On the other hand, when mutable objects are passed as arguments to a function, they can be modified directly within the function and these changes affect the original object outside the function.


Integer caching

Is an optimization in CPython, the reference implementation of Python in C, that caches a number of common integers, known as small integers, in a predefined range.

Specifically, CPython preallocates and stores in memory the first 262 integers, ranging from -5 to 256. This means that when you create a variable and assign one of these integers to it, you are actually referencing the object for that integer that is already stored in memory, rather than creating a new object each time.

No hay texto alternativo para esta imagen

要查看或添加评论,请登录

社区洞察

其他会员也浏览了