Mutable & Immutable Python objects

After 3 months of learning C at the Holberton School in San Francisco, we have recently started to learn Python. It has been really enjoyable and challenging in a different way than C was. This week we learned about python classes and objects and how everything in python is really an object. In the following paragraphs, I’d like to summaries some of the things I learned in the past few days.

Let’s say we start out with creating 2 identical lists a and b. We can use the builtin function id() to return the “identity” of a and b. The returned id is a number which is guaranteed to be unique and constant as long as our object exists. Fun fact, in the CPython implementation, the id number is actually the address of the object in memory.

So every object we create in Python has an id, and it also has a type. Using the built in type function we are told the type of any of our objects.

As we see here, all those types, list, int, float.. are actually classes. And what we find out is that each class has certain operations that it can perform on its instances. For example the append method in the list class adds an item to the end of the list.

When it comes to python types, some of them are mutable, or can be changed and some of them aren’t. Take a look at the following code:

Lists it turns out are mutable. We changed the first item in list a and that was reflected in b, because a and b are both pointing to the same list in the same place in memory. But when it came to c and d which are ints, we see that when we assign 5 to c we are changing the memory location c is pointing to, but that has no effect on d, which still has the value 3. That’s because integers in python are immutable. Take a look at this helpful list:

So lists, sets and dictionaries in python are mutable objects and everything else is not.

So why is mutability important? It actually has to do with memory and efficiency. Take a look at the code bellow:

Because str1 is a string which is immutable, every time we change it we are freeing and reallocating memory, which is not very efficient. In contract, lets see what happens when we modify a list:

Now that str1 is a list, it is modified in place, and that is much more efficient than all the memory manipulations that happened when it was an immutable string.

What about passing arguments to functions? In C we could pass arguments by value or by reference, but in python it’s a little different. When a value is passed to a function and has it’s value changed inside the function, it’s value outside the function changes depending on whether it is mutable or not. Example:

We see above that a string modified inside a function remains unmodified outside of it, a list on the other hand is changed outside of the function after it’s been modified inside it. All this is because a list is mutable and a string is not.

So finally, in Python it is very important to understand object mutability. Make sure you understand how and why it works!