Mutable, immutable… Everything is an object.

Daniel Lorenzo Scoccia
5 min readOct 6, 2020

One of the most powerful features of Python is that everything is an object, even functions. Every object has its own data type and internal state (data).

In this example, when Python executes “b = 500”, creates an integer object at memory location '0x7fee2813f230' that stores the value 500, and“b” holds the memory address to this object.

Now, “b = 800”, a new integer object it’s created with different value and different memory address, which is now stored in b. But what happens if we execute a new instruction: “a = b”, in this case a will reference to the same object that b is pointing, therefore it will have the same address.

Referencing the same object

In the above example, we assign b to a. This is known as aliasing, and it happens whenever one variable’s value is assigned to another variable because variables are just names that store references to objects.

Aliases

Some examples of objects in Python are integers, strings, lists, functions, floats, etc…

In our first example what we did was binding a name to an object. And this implies that you can bound multiple names to a single object.

Each object is stored at some memory location. The id() function returns the memory location of the object referenced by a variable. So this function can be used to check if the objects are pointing to the same memory location in other words the same data in the memory.

id() is a builtin function that accepts one parameter and is used to return the identity of the object.

Syntax:

id(object)

While type() is a builtin function that returns a class type of an object passed as a parameter. Let’s see an example of how to use it:

Some type() outputs

These two functions will help us to find if we are dealing with the same or different objects, and also to understand more our code.

The value of mutable objects can be changed after they are created. The value of immutable objects cannot be.

Mutable:

  • Lists
  • Sets
  • Dictionaries

Since lists, sets and dictionaries are mutable, we can add, delete & modify its contents. In all the three examples below, the memory location did not get changed as expected during the operation. This confirms that lists, sets & dictionaries are mutable objects.

Immutable:

  • Numbers (int, float, decimal, boolean, etc.)
  • Strings
  • Tuples

Since numbers, strings, tuples, and also frozen sets are immutable, the internal state (data) can not be changed. Instead, the memory address gets changed because a new object gets created at a different memory location during the process. It also offers greater security than mutable objects.

There are two curious cases, the first one is when you have mutable objects inside an immutable one. For example, even though tuples are immutable, the elements that it contains can be mutable.

Tuple of lists

The second case is because an optimization, CPython pre-allocates some integers deemed small as they are used the most, and will reuse them instead of instantiating duplicates. In Python 3.4, the numbers [-5, 256] are pre-allocated.

Let’s see whats happens if we try the same but using a number outside de pre-allocated range.

In order to save up memory space, python takes advantage of the immutability of some of its objects in order to reduce memory occupation in the system.

When an object is passed to a function, a new reference to it is created instead of a copy. This happens with both mutable and immutable objects. Arguments are always passed to functions by reference in Python. The caller and function code blocks share the same object.

As with immutable types, when a mutable object is passed to the function, a copy isn’t created, but instead a new reference to the object. If there is a modification inside the function, this change will be notable outside too.

Knowing how to use properly both mutable and immutable objects is important and allows us to decide the data type that fits the task.

“Immutable objects are simple. They can only be in one state, which is carefully controlled by the constructor. One of the most difficult elements of program design is reasoning about the possible states of complex objects. Reasoning about the state of immutable objects, on the other hand, is trivial.” ― Brian Goetz

--

--

Daniel Lorenzo Scoccia
0 Followers

Full Stack Software Developer. Freelancer.