DEV Community

Cover image for Python !!! Mutable and Immutable objects
sanin_mohd
sanin_mohd

Posted on

Python !!! Mutable and Immutable objects

Points To Remember

  1. Objects are stored in memory
  2. Variable are used to refer object
  3. Int, Float, String, Tuple, Set, list etc are inbuilt classes
  4. 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
Enter fullscreen mode Exit fullscreen mode

Image description

Different types of objects in python

  1. 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]
Enter fullscreen mode Exit fullscreen mode

now, we print address id of x pointing

   print(x)
   print("Object address id x pointing : ",id(x))
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

now, we print address id of x pointing

   print(x)
   print("Object address id of x : ",id(x))
Enter fullscreen mode Exit fullscreen mode

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._

  1. 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"
Enter fullscreen mode Exit fullscreen mode

"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"
Enter fullscreen mode Exit fullscreen mode

Run Program And See Output
H

Now, we change 0th index value "H" to "A"
let's see what happens

  x[0] = "A"     
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

now, we print address id of x pointing

   print(x)
   print("Object address id x pointing : ",id(x))
Enter fullscreen mode Exit fullscreen mode

Run Program And See Output

 HAPPY
 Object address id x pointing :  140210683580720
Enter fullscreen mode Exit fullscreen mode

here, we change x i.e

   x = "hAPPY"
Enter fullscreen mode Exit fullscreen mode

now, we print address id of x pointing

   print(x)
   print("Object address id of x : ",id(x))
Enter fullscreen mode Exit fullscreen mode

Run Program And See Output

 HAPPY
 Object address id x pointing :  140210683580720
 hAPPY
 Object address id of x :  140210683580592
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

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

  1. Objects are stored in memory
  2. Variable are used to refer object
  3. Int, Float, String, Tuple, Set, list etc are inbuilt classes
  4. Int object stores integer type data

Top comments (0)