Mutable and Immutable Objects in Python
An object whose internal state can be changed is mutable. On the other an immutable object cannot be changed
once it has been created.
List of Mutable and Immutable objects
Objects of built-in type that are mutable are:
Lists
Sets
Dictionaries
User-Defined Classes (It purely depends upon the user to define the characteristics)
Objects of built-in type that are immutable are:
Numbers (Integer, Rational, Float, Decimal, Complex & Booleans)
Strings
Tuples
Frozen Sets
User-Defined Classes (It purely depends upon the user to define the characteristics)
In Python, everything is treated as an object. Every object has these three attributes:
Identity – This refers to the address that the object refers to in the computer’s memory.
Type – This refers to the kind of object that is created. For example- integer, list, string etc.
Value – This refers to the value stored by the object. For example – List=[1,2,3] would hold the numbers
1,2 and 3
While ID and Type cannot be changed once it’s created, values can be changed for Mutable objects.
Displaying the memory address of an object
Note: The built-in function call id(objectX) returns the address an objectX in decimal. To obtain that address in
hexadecimal, use the call hex(id(ObjectX))
Mutable Objects in Python
cities = ["Riyadh", "Dammam", "Taif"]
print(cities)
print("Address:", id(cities))
cities.append("Jubail")
cities[1] = "AlKhobar"
print(cities)
print("Address:", id(cities))
Possible output:
['Riyadh', 'Dammam', 'Taif']
Address: 1525069512448
['Riyadh', 'AlKhobar', 'Taif', 'Jubail']
Address: 1525069512448
Note: Because the list cities is mutable, it’s does not change even after it is modified.
---------------------------------------------------------------------------------------------------------------------------------------
Page 1 of 3
gradesDictionary = {"Yusuf":56.5, "Ayman":75.0, "Luai": 80.0}
print(gradesDictionary)
print(id(gradesDictionary))
gradesDictionary["Ayman"] = 90.0
gradesDictionary.update({"Yusuf":72.0})
print(gradesDictionary)
print(id(gradesDictionary))
Possible output:
{'Yusuf': 56.5, 'Ayman': 75.0, 'Luai': 80.0}
1905046595712
{'Yusuf': 72.0, 'Ayman': 90.0, 'Luai': 80.0}
1905046595712
Note: Because gradesDictionary is mutable, it’s address does not change even after it is modified.
---------------------------------------------------------------------------------------------------------------------------------------
Immutable Objects in Python
x = 5
print(id(x))
x = 34
print(id(x))
Possible output:
2286551132592
2286551133520
Note: Because objects of type int are immutable, a different object is created by the second assignment.
---------------------------------------------------------------------------------------------------------------------------------------
city = "Riyadh"
print(city)
print("Address:", id(city))
city = "Jubail"
print(city)
print("Address:", id(city))
Possible output:
Riyadh
Address: 2491445411184
Jubail
Address: 2491445411376
Note: Because objects of type string are immutable, a different object is created by the second assignment.
---------------------------------------------------------------------------------------------------------------------------------------
city = "Riyadh"
city[3] = 'A'
print(city)
Exception raised:
TypeError: 'str' object does not support item assignment
Note: Trying to modify a string will raise a TypeError exception
---------------------------------------------------------------------------------------------------------------------------------------
Page 2 of 3
myTuple = (1, 4, 5, 7)
print(myTuple)
print(id(myTuple))
myTuple = (14, 40, 30)
print(myTuple)
print(id(myTuple))
Possible output:
(1, 4, 5, 7)
2637749736208
(14, 40, 30)
2637749721856
Note: Because tuples are immutable, a different object is created by the second assignment.
-----------------------------------------------------------------------------------------
myTuple = (1, 4, 5, 7)
print(myTuple)
myTuple[2] = 60
TypeError: 'tuple' object does not support item assignment
Note: Trying to modify a tuple will raise a TypeError exception
---------------------------------------------------------------------------------------------------------------------------------------
Note: For efficiency purpose, immutable objects that have the same value(s) are given the same address i.e.,
they refer to the same object.
x = 20
print(id(x))
y = 20
print(id(y))
print()
string1 = "Dhahran"
print(id(string1))
string2 = "Dhahran"
print(id(string2))
print()
tuple1 = (1, 7, 8)
print(id(tuple1))
tuple2 = (1, 7, 8)
print(id(tuple2))
Possible output:
2664904944528
2664904944528
2664910749168
2664910749168
2664911051520
2664911051520
Page 3 of 3