Python !!! Mutable and Immutable objects
sanin_mohd
Posted on February 5, 2022
Points To Remember
- Objects are stored in memory
- Variable are used to refer object
- Int, Float, String, Tuple, Set, list etc are inbuilt classes
- Int object stores integer type data
Example
x = int(1) # int class constructor
x = 1
# Both expressions are same
# x is a pointer and 1 is an object
Different types of objects in python
- Mutable objects
A mutable object is an object whose state can be modified
after it is created
List, dictionary, set and user-defined classes are mutable objects.
Example
Let's create a list
x = [1,2,3,4,5]
now, we print address id of x pointing
print(x)
print("Object address id x pointing : ",id(x))
Run Program And See Output
[1, 2, 3, 4, 5]
Object address id of x : 140169509393728
now, we change first element of the list i.e
x[0] = 9
now, we print address id of x pointing
print(x)
print("Object address id of x : ",id(x))
Run Program And See Output
[1, 2, 3, 4, 5]
Object address id of x : 140169509393728
[9, 2, 3, 4, 5]
Object address id of x : 140169509393728
Since list is mutable object, We can see that, before and after changing object, Both address ids are same._
- Immutable Objects
A immutable object is an object whose state can't be modified
after it is created
Int, Float, String, Tuple, decimal, bool and range are Immutable objects
Example
Let's create an String object "Hello"
x = "Hello"
"Hello" is an object of type string
x is a pointer to the object "Hello"
Now, let's see
print(x[0]) #this code will print 0th index value "H"
Run Program And See Output
H
Now, we change 0th index value "H" to "A"
let's see what happens
x[0] = "A"
Run Program And See Output
H
Traceback (most recent call last):
File "main.py", line 3, in <module>
x[0]="A"
TypeError: 'str' object does not support item assignment
Here programs throws an error ,Telling that TypeError: 'str' object does not support item assignment, which means string is Immutable.
we can only create and delete immutable objects, we can't change immutable objects
One tip to boost up your knowledge
let's create another string object
x = "HAPPY"
now, we print address id of x pointing
print(x)
print("Object address id x pointing : ",id(x))
Run Program And See Output
HAPPY
Object address id x pointing : 140210683580720
here, we change x i.e
x = "hAPPY"
now, we print address id of x pointing
print(x)
print("Object address id of x : ",id(x))
Run Program And See Output
HAPPY
Object address id x pointing : 140210683580720
hAPPY
Object address id of x : 140210683580592
We can see that ,Both address ids are different, Which means here "HAPPY" is not changed to "hAPPY", But 2 separate string objects are created.
One Last Tip
let's create 2 variables with
x = "HAPPY"
y = "HAPPY"
now, we print address id of x,y pointing
print(x)
print("Object address id x pointing : ",id(x))
print(y)
print("Object address id y pointing : ",id(y))
Run Program And See Output
HAPPY
Object address id x pointing : 140210683580720
HAPPY
Object address id y pointing : 140210683580720
Here, both x and y address ids are same, which means both x,y are pointing towards the same object
Points To keep in mind
- Objects are stored in memory
- Variable are used to refer object
- Int, Float, String, Tuple, Set, list etc are inbuilt classes
- Int object stores integer type data
Posted on February 5, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 3, 2024
November 14, 2023