Mutable and Immutable objects

Juan Felipe Prado Cruz
4 min readMay 27, 2021

--

A decription image that holds the title of the article

As we learning more, we have discovered that everything in Python is an object. And that an object can be mutable or immutable.

Before seeing what is mutable or immutable, first, we need to understand how variable works in Python. The functionality of variables are a bit different from others languages. In Python, a variable is a reference to an object. It means that a variable holds the address of an object. And this object has its own name assigned internally by Python, and you can access it by function id()

Id() and Type()

If we want to check what is the specific name that the variable stores we use the function id(). And if we want to see what type of object does the variable holds, we use type().

```id and type example```
>>> a = 3
>>> id(a)
9785024
>>> type(a)
<class 'int'>
>>> b = "string"
>>> id(b)
140166500028848
>>> type(b)
<class 'str'>

As we could see from Python’s interpreter, when we assign 3 to the variable a we could see the address where it was stored with id(a) = 9785024and then check what type of object it type(a) = <class 'int'> .

Immutable objects

An immutable object is one that the value can not be changed. the created new memory when their value has changed. some of these objects areint, float, string, bool, tuples

``` Example of immutable ```
>>> a = 9
>>> id(a)
9785152
>>> a += 1
>>> id(a)
9785184
--------------------------
>>> b = "Hello"
>>> id(b)
140166474379760
>>> b += " World"
>>> b
'Hello World'
>>> id(b)
140166474409264
>>>

When we change the value of a string or int object, the new value were stored in a new allocation of memory.

Mutable objects

A mutable object let us change it value and it would not create or allocate a new space in memory. Diccioncaries, Lists and sets are some examples of mutable objects.

```Example mutable
>>> a = [1, 2, 3]
>>> id(a)
140166476113472
>>> a[1] = 9
>>> a
[1, 9, 3]
>>> id(a)
140166476113472

when changing the value of a, we can check that the id of it did not change at all, this it because a list is a mutable object.

So, why does matter to know the different between these two?

Because handle these two differently, let’s another example of mutable objects

```Example of 
>>> a = [1, 2, 3]
>>> b = a
>>> b[1] = 9
>>> b
[1, 9, 3]
>>> a
[1, 9, 3]
>>> id(a)
140259022532096
>>> id(b)
140259022532096

let’s say that you create a list in a and you have b = a as remainder a variable holds theaddress reference of an object, if we want to change the value of b it would also change into a since bhave the reference of a and it is a list, it would not create another sapce memory because list are mutables. So this might give you some not expect the result. If you want them to change in both variables this would be great, But if you want to have them separately, then you can create a clone of a by using the method of copy

``` Example 
>>> a = [1, 2, 3]
>>> b = a.copy()
>>> b
[1, 2, 3]
>>> id(a)
140259022532032
>>> id(b)
140259022603328

we copy() method bvariable would hold the same as variable a , so now it we change something in b it won’t be changed in a.

So, Mutable objects are a good option to use when you need to change something of the object. Immutables are used when you want to ensure that the object you made will always stay the same.

How arguments are passed to functions

When passing the arguments to a function, the object passed would have different behavior depending on if mutable or immutable. Calling an immutable object by reference in a function, when changing its value, it won’t change and would create a space of the memory of that new value. But using a mutable object by reference, when changing the value it would change in the function and where it was declared

```Example of a immutable object passed into a function```
>>> def newValue(a):
... print(id(a))
... a = a + 1
... print(a)
... print(id(a))
>>> a = 3
>>> id(a)
9784960
>>> newValue(a)
9784960
4
9784992
>>> a
3
>>> id(a)
9784960
>>>

When we passed a to the function, the id for the a still the same, but when changed the value into the function it would create another id, but outside of the function we see that a still have the same id.

```Example of a mutable object passed into a function```
>>> def newValueList(a):
... print(id(a))
... a.append(4)
... print(a)
... print(id(a))

>>> a = [1, 2]
>>> newValueList(a)
140258997607296
[1, 2, 4]
140258997607296
>>> print(a)
[1, 2, 4]
>>> id(a)
140258997607296

So, when using a mutable object we could see in the above example that always it going to have the same id.

Now I hope you understand more about how works mutable and immutable, and how to use them efficiently.

--

--

No responses yet