Python3: Mutable, Immutable… everything is object!

In Python everything is an object because it can be assigned to a variable or passed as an argument to a function. Strings, lists, functions and even modules are objects. In the Guido van Rossum’s words, he said … “One of my goals for Python was to make it so that all objects were ‘first class.’ By this, I meant that I wanted all objects that could be named in the language (e.g., integers, strings, functions, classes, modules, methods, etc.) to have equal status. That is, they can be assigned to variables, placed in lists, stored in dictionaries, passed as arguments, and so forth.

Id and type

Id, this function gives us the identity of an object, the function receives a unique parameter, and we use it to know the unique identifier of each object, in C language it would be the memory address. As you can see, in the example each estring has a different id, that is because each is a different object.

Type, this function gives us the type of variable that it is handling, that means, to which class an object belongs. In example ‘a’ is an integer and belongs to the int class, ‘b’ is a list and belongs to the list class, ‘c’ is a tuple belongs to tuple class and ‘d is a string and belongs to the string class.

In python there are two types of operators that allow us to evaluate the value and the ‘id’ the objects, the logical operator “==” which compare if two objects have the same value, if so, True will return and otherwise return False and the operator ‘is’ evaluating whether two objects have the same ‘id’ if so, it will return True otherwise it will return False.

Mutable and inmutable objects

Mutable objects can be modified through their attributes, while immutable objects once created do not allow to be modified.

In the case of Python tuples have a surprising trait: they are immutable, but their values may change. This may happen when a tuple holds a reference to any mutable object, such as a list.

Why does it matter and how differently does Python treat mutable and immutable objects?

Its important to know how differently python treat mutable and immutable types, especially when they are passed onto functions, because memory efficiency is affected, it depends on the appropriate objects used.

If a mutable object is referenced to a function, it may change the original variable itself. Hence, to avoid this, the original variable must be copied into another variable. So, in the other hand, immutable objects cannot change their value. Accessing an immutable object is a quicker process since it stays the same as when it was first instantiated but when you want to change it the drawback makes an appearance. It will cost more resources from the system because it reserves a new space in memory. A mutable object is handled in such a way that it can be harder to process due to it’s ever-changing capacity and quicker to modify since no additional memory space is required.

In short, a function can call a variable by value or by reference. The first one, copies the variable and the second one the variable’s id is passed. To avoid changes in the original value of a mutable variable, it must be passed by reference to the function and the immutable variable can be passed by value safely since it cannot be modified.

--

--

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