Python !!! Mutable and Immutable objects
Tags: python, oop, programming
Published: 2022-02-05 16:17:29



> # Points To Remember 
> 1. Objects are stored in memory
> 2. Variable are used to refer object
> 2. Int, Float, String, Tuple, Set, list etc are inbuilt classes 
> 3. 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
     
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfo7zwac1nk755ma2azt.png)

## 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]

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

2. **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
> 1. Objects are stored in memory
> 2. Variable are used to refer object
> 2. Int, Float, String, Tuple, Set, list etc are inbuilt classes 
> 3. Int object stores integer type data