Mutable, Immutable... everything is object!

Mutable, Immutable... everything is object!

So, I just recently woke up to the truth that in Python everything is an object, meaning the no discrimination of classes in this language, but... What is actually an object? Can they be changed? If so.. Which kind? In this blog I will approach the subject in the most possible simple terms hoping to bring the reader an insight while I increment my understanding by the doing of this blog, which demanded to research, think, rethink and and a brainstorm synergy achieved with my peers at some points of a whole day of studies.

In programming an Object is defined as abstract data type that can include different properties, methods, modules and other objects, created by the programmer. Python manage some flexibility by allowing the assignment of anything into a variable or passing arguments to a function.

Now we know that everything in Python is an object, we need to know what kind they are. A mutable object can be changed after it is created, and an immutable object can not. Objects of built-in types like (int, float, str, tuple, bool, unicode) are immutable and built-in types like (list, set, dict) are mutable.

No alt text provided for this image


How do we know if our Variable is a Mutable or Immutable Object?

As in Python everything is an Object, every variable holds an object instance. When we initiate an object, it is assigned to an object id. The type is defined at runtime, when it is set, it can never be changed,but its state can be changed if it is mutable. Every object has an identity, a type, and a value.


id() Function

This function can be seen as the unique and constant identity number of the object (an integer) while its life, that usually corresponds to the object’s location in memory.

In the example below, k, l and c are integer variables with a value, meaning that is a type int (integer) that comes from the class that it was instantiated at. The output is a unique number id, provided by the builtin function id():

No alt text provided for this image


type() Function

Is a built-in method in Python, used to determine the type of the variable used at runtime. If a single argument (object) is passed to type(), it returns the type of the given object. If three arguments are passed (name, bases, and dict), it returns to a new object type. In the example below each variable has a different type which is indicated after running function type:

No alt text provided for this image

Immutable Objects Example

These are simply objects that can’t change. Two types of immutable objects in Python are int, string, float, bool and tuple. In the example below we see a sting as an output, in this kind of data is not possible to change its value, what we can do is to assign a new string to that same variable.

No alt text provided for this image


Mutable Objects Example

They can be list, dict, set, byte array. Mutability is important for choosing data structures for a program because they are used to store chunks of data. So if you have a collection of 20.000 names that you want to safe, you can store them in an immutable tuple.In the simple example below the tuple t contains different data types elements, the first one is an immutable string and the second one is a mutable list.

tuple t = (‘friends’, [1, 2, 3, 4, 5, 6...])


How objects are passed to Functions

They are able to make these changes because they are being passed a reference of the object. In Python, that is the same as the object memory address. This is known as “call by sharing”. Immutable objects are also passed around by reference to them.

In the example below the same object is passed to the function, but the variables value stay the same even when the object is just as the previous one. The value is called by the function if the value of the variable is passed , not the object itelf, the variable referencing the object does not change but the object itself is being changed within the scope of the function.



def updateNumber(k):
    print(id(k))
    k += 10c = 38
print(id(c))                   # 10055680
updateNumber(c)                # 10055680
print(c)                       # 38


In conclusion we know that Python treats everything as an object and that all objects have an id and type. An object value can be changed but not its id and type. When we pass by sharing that means that mutable objects are passed around in functions, so the caller and called functions can modify the object.

Immutable objects have no such methods, and therefore cannot be changed. When immutable objects are being passed around functions it is by reference to the object, so unless the return value is exactly the passed argument, it will return a new object.


If you have any comment, suggestions or views on my blog please tweet me @KarenCa96752258


To view or add a comment, sign in

More articles by Karen Campo

  • MoneyManager

    An App meant to help you in the management of your finances Hello there readers, welcome to the future of saving! Let…

  • The Magic Behind an Internet Search

    We are constantly browsing in our search bar for anything, it is just part of our daily lives, just as essential as in…

  • A personal view of the Internet of Things

    It is the end of the year and of the world as we know it, almost, and I am here with my most faithful companion in…

  • Static Libraries vs. Dynamic Libraries

    As programmers (student in my case) we must solve large problems, the best way is by breaking them down into smaller…

  • What Happens when you type ls -l in the shell

    So, let’s break it down by parts: First let's quickly check in a few words what is essentially a Shell and a definition…

    1 Comment
  • Worst abuse of the C preprocessor (IOCCC winner, 1986)

    3rd International Obfuscated C Code Contest (1986) It is an annual programming contest with a obfuscated C code. They…

  • WHAT IS A C LIBRARY AND HOW DOES IT WORK

    In C libraries are very important because C language just supports the most basic features needed. For example, C…

  • What Happens when you type gcc main c

    First we need to know what gcc means, gcc stands for GNU Compiler Collection, GCC is a collection of programming…

  • WHAT HAPPENS WHEN YOU USE ls *.c

    When I execute command ls *.c nothing happened because I'm just using an extension for language like C, C++ but is not…

Insights from the community

Others also viewed

Explore topics