Tuple says NO to Mutability !

We live in a world where mutables are liked by Kings as they never dare to take an un-popular stand and continue to walk around with fake smiles.

I want to choose Tuple as my hero in Python as it chooses to be an immutable living among mutables. Today we will be discussing about Python mutability over basic types.

Immutable means “Which can’t be muted”. In python everything is an object and immutable object just mean that “Once the object is created it can’t be changed”.

Immutable objects provide thread safety and improves clarity.

We will go on a journey and find the mutability in basic python data types.

Our guide in this journey will be id() function.

help(id)OUTPUTid(obj, /)     
Return the identity of an object.

This is guaranteed to be unique among simultaneously existing objects. (CPython uses the object's memory address.)
  1. Integer Data

We will be creating an integer object and then will try to assign a new value to that object.

a = int(123)
print(a)
print(a,’a is in memory location’,id(a))
a = -256
print(a)
print(a,’a is now in memory location’,id(a))
OUTPUT123
123 a is in memory location 93920955447616
-256
-256 a is now in memory location 140660329473264

We can see that both the values and the memory locations are also changed. This clears that both the values 123 and -256 are stored in different locations. While we tried to assign a new value, just the reference is changed to a new location. Hence it proved that Integer objects are not mutable.

2. Float Data

a = float(0.0)
print(a)
print(a, ’a is in memory location’ ,id(a))
a=1.1
print(a)
print(a, ’a is now in memory location’ ,id(a))
OUTPUT0.0
0.0 a is in memory location 140660329472464
1.1
1.1 a is now in memory location 140660329472240

Here also we can see that reassignment was unable to rewrite the float object, but just referenced to another location to store the new data. So Float object is also immutable.

3. String Data

a = str(123)
print(a)
print(a,’a is in memory location’,id(a))
a=’2@46'
print(a)
print(a,’a is now in memory location’,id(a))
OUTPUT123
123 a is in memory location 140660329541744
2@46
2@46 a is now in memory location 140660329581616

This is similar to the Integer and String Data operations, we have done above.

In String we can access the characters using index starting from 0. So let's try to assign values using the index.

a[0]=’A’print(a)print(a,’a is now in memory location’,id(a))OUTPUTTypeError: 'str' object does not support item assignment

Item assignment gives us an error, so String is also Immutable.

4. List Data

a = [1,0+0j,-89,’@’,[],(1,2,3),{1,2},{1:2}]
print(a)
print(a,’a is in memory location’,id(a))
a=[1,2,3,4,5]
print(a)
print(a,’a is now in memory location’,id(a))
a[0]=[1,’#’,4]
print(a)
print(a,’a is now in memory location’,id(a))
OUTPUT[1,0j,-89,'@',[],(1,2,3),{1, 2},{1:2}]
[1,0j,-89,'@',[],(1,2,3),{1, 2},{1:2}] a is in memory location 140660329255552
[1, 2, 3, 4, 5] [1, 2, 3, 4, 5] a is now in memory location 140660329442352
[[1, '#', 4], 2, 3, 4, 5]
[[1, '#', 4], 2, 3, 4, 5] a is now in memory location 140660329442352

Here we have rewritten the List object once and changed the first element with another list. This shows us that List data is mutable.

5. Tuple

d=[1,2,3,[1,-7]]
a = (1,0+0j,-89,’@’,d,(1,2,3))
print(a)
print(a,’a is in memory location’,id(a))
a=(1,2,3,4,5)
print(a)
print(a,’a is now in memory location’,id(a))
OUTPUT(1, 0j, -89, '@', [1, 2, 3, [1, -7]], (1, 2, 3))
(1, 0j, -89, '@',[1, 2, 3, [1, -7]],(1, 2, 3))a is in memory location 140660330076368
(1, 2, 3, 4, 5)
(1, 2, 3, 4, 5) a is now in memory location 140660330757680

Here also we can see that location is also changing with the data values

As tuple is a sequential data type, we can access values on the basis of the index. Let’s try to assign some values in that way.

a[0]=[1,’#’,4]
print(a)
print(a,’a is now in memory location’,id(a))
OUTPUTTypeError: 'tuple' object does not support item assignment

Still, we have a loophole to change the Tuple to a limit, as it holds a list which can be changed.

d=[1,2,3,[1,-7]]
a = (1,0+0j,-89,’@’,d,(1,2,3))
print(a)
print(a,’a is now in memory location’,id(a))
d[0]=0
print(a)
print(a,’a is now in memory location’,id(a))
OUTPUT(1, 0j, -89, '@', [1, 2, 3, [1, -7]], (1, 2, 3))
(1, 0j, -89, '@', [1, 2, 3, [1, -7]], (1, 2, 3)) a is now in memory location 140660329667504
(1, 0j, -89, '@', [0, 2, 3, [1, -7]], (1, 2, 3))
(1, 0j, -89, '@', [0, 2, 3, [1, -7]], (1, 2, 3)) a is now in memory location 140660329667504

Here we not only changed the tuple using that list and the memory location is also the same. It shows that a tuple object is both mutable and immutable depending on the situation.

Tuples holds its immutability and mutability and that makes it a unique among other data types.

Thats all!

Wish you all the best for future learning !!

--

--

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