Everything is object in Python (Mutable, Immutable…)

In python almost everything is an object, today in this article i’ll talk about the different data type, how know the type and get the id of a data, we’ll also see immutable and mutable object, how python treat mutable/immutable object (assignment, aliasing..), how argument of function are treated and a bonus.

  • First, the data type in python are list, dictionary, tuple, string, int, float…all of these are object.
data types
  • How can i know what type are a data and how i can know his id in memory ?

To know the type of a data, you can use the function type(), it will return the type of the object passed, as every data type are object in python all these object are related to a class, this is what we see in the example below. This function is really useful to be used commonly to test the type of data type can be passed to a function as example.

type()

To get the ID of a data, you can use the function id(), it will return the identity of your object (your data type):

id()

But what is really the id of a data ?

The id is the identity of the data type, it will be unique and constant during its lifetime but in reality this id in Cpython () is the adress of the object in memory. We will see after that multiple data type can have the same id.

  • Every data type can be really modified without being copied ?

Now it’s the time to enter into the concept of mutable and immutable object. In reality, all these data type are not the same except that there are object, many of them cannot be modified directly, this is what we call immutable. The data type that are immutable are : string, int, float, tuple and other are mutable : dictionary, list.

what means really a data type that is immutable ?

It means that is data type cannot be modified without being copied or being assigned again. As example an integer, if i decide to change the value of my variable integer a new object with a new identity will be created :

integer

The same to all immutable data type but you might think that you if you have already one time modify a string just like this :

Modifying a string

Wow, the result seems to conclude that a string can be modified and are not immutable…but if you we look closer to the id of the string if it’s the same id before and after the modify :

The id has changed while being modified, a new assignation of a new object of string has been done so a new string has been created to do the modify.

Now, let’s take a look if we change a mutable data type to see what that does, we’ll take a list for the example :

example mutable list

The change has been done and the id of the object list has not been changed, so the object has been modified and not been new assigned as immutable.

But, it exists more things to know about how python treat mutable and immutable object.

  • How python treat mutable and immutable object ?

As we saw python treat object in function of mutable and immutable object, object will be new assigned if you want to change an immutable object and if you want to change a mutable object no new assignation will be done. But we saw example only with one variable, let’s do some manipulation with mutable and immutable object to see what’s changed or not.

First take as example, two string with the same value, the id of each should be different or not ?

Two immutable string

So…. each string point to the same id, why 🤔?

In fact, Python to optimizes ressources will refers these two variables with the same value to the same object, because string are immutable, no risk to be changed, that works for all the immutable data types and only (not really because aliasing) when the value is the exact same value for each.

as example, a mutable data type a list :

two mutable object

Each list refers to one single object and not the same one.

And what happens if i assign one mutable object variable to another mutable ?

aliasing two mutable object

It refers to the same object has as immutable, one changes with one alias will change the other one.

This behavior can be useful, it is sometimes unexpected or undesirable. In general, it is safer to avoid aliasing when you are working with mutable objects. Of course, for immutable objects, there’s no problem. That’s why Python is free to alias strings when it sees an opportunity to economize.

Before end this chapter, one immutable can be “changed”, this the tuples. A tuple can be changed but only the value inside the tuple and only if it’s a mutable one.

  • How python works with variables passed in argument of functions ?

If you try to pass a variable (immutable) into the argument of a function and try to change his value, you might see that your original variable passed has not changed. In fact, Python doesn’t pass the reference of the variable or the value but assign the value to a new object, in other programming language you can passes the reference of a variable and change the value of the variable in the function and the changement will affect the original variable, in python it’s not like that.

You can see with this example that, the function create a new object and assign the value passed to the function and not changed the original variable passed in the argument.

If you want to have the same state of passing the reference of a variable you need to do like this :

Returning and re-assigned the new value to your variable is the possible way to do like reference as in C as example.

The case of mutable data type, the function assign the value to a new variable that refers to the same object, the variable in the function has the same id as the original one. You can directly change the value of the original variable in the function, as example with a list and with the method .append() :

  • Bonus : Range integer -5 to 257

In fact, this is not exactly correct for integers what i said before, to a range of number of -5 to 257, no object of integers will be really created, why ?

Python use two MACRO named : NSMALLPOSINTS(0 to 257)and NSMALLNEGINTS(-5 to 0). Python keeps an array of integer object to all integers created in this range of number, we’ll get only a reference to the existing object in memory, python use this method because this is the most used integers, it will be more faster.

This is why, if you create two integers like :

Same value but not the same id because the integers are out of range, two new different integers object will be created.

That’s is for today, thanks to have listening me, i hope your learn something today :)

Stay tuned.

Software engineer student at HolbertonSchool 👉https://github.com/ThibautBernard 👉https://twitter.com/__ThibautDev

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