4. Python Mutable & Immutable Concept


 

Fundamental data types with an immutable concept:

Fundamental data types are int, float, complex, bool, str.

Mutable means a changeable object or given value or data.

Immutable means an unchangeable object or given value or data.

Once we create the object, we can’t change that object. If we are trying to change the object with those changes, a new object will be created, in this non-changeable nature is called an immutable.

Now take the example of int data type. Now we are trying to change

number = 10

number = number +1 # it’s changed without any error

now we think (number +1) is replaced with 10, but it’s not happened. (number +1) the new object will be created. Now onwards (10) represents to (number+1). Now 10 value is erased by python virtual machine (PVM). Check some examples. 10, number +1 ids are different.

number = 8

print(id(number)) 

1692903456   #result

number = 10

print(id(number))

1692903488    #result

Both are different IDs.

Imp Note: In python, everything is the object.

One more example

number = 10

number1 = number

Now both IDs or addresses are the same.

Now we are creating a new object like

number1 = 11

Then number1 value (11) has got changed with a new id or address.

Now number, number1 are different IDs. Check example.

 number = 10

print(id(number))

1692903488    #result

Number1 = 11

print(id(number1))

1692903504   #result

why the immutable concept is required. check with some examples

number = 10

number_1 = 10 

number_2 =10

three are the same values. These ids are created by PVM. In python PVM is smart. That’s why PVM creates only one id for three values. Because all are the same values.

PVM is smart, if we are creating one variable with the same value, PVM creates one id for that value. If we create the same value with another variable then PVM doesn't create the new id. This is pointing to the existing same id.

Ex:

number = 10

print(id(number))

1692903488    #result

Number1 = 10

print(id(number1))

1692903488    #result

Number2 = 10

print(id(number2))

1692903488    #result

# all Id’s values are same.

Advantage:

For memory saving for id creation and performance also increasing. this type of ID creation is not in int only type, other types like float type, complex type, bool type, and str type.

Check some examples.

Int type we already see that.

Float type Ex:

number = 10.12

print(id(number))

20201072  #result

number1 = 10.12

print(id(number1))

20201072  #result

 number2 = 10.12

print(id(number2))

20201072  #result

Complex type Ex:

number = (10+12j)

print(id(number))

23240736  #result

number1 = (10+12j)

print(id(number1))

23240736  #result

number2 = (10+12j)

print(id(number2))

23240736  #result

bool type Ex:

number = True

print(id(number))

1692778816 #result

number1 = True

print(id(number1))

1692778816 #result

number2 = True

print(id(number2))

1692778816 #result

str type Ex:

number = ('srinu')

print(id(number))

23494048  #result

number1 = ('srinu')

print(id(number1))

 23494048  #result

number2 = ('srinu')

print(id(number2))

  23494048  #result

take one more example in a Real-World scenario

to take a new online gas connection form look like below




First distributor name: we are creating all of the city distributor names in immutable methods so the user enters any two to five letters it will show automatically the distributor name. and we just select that name.

Advantage: for lac of people only select 200 or max 300 of distributors name is only. So our application takes very little storage.

Photo: this is section IMG or png format type, so we can’t immutable this option.




This is the section we can immutable easily with only 3 options. So that user selects these options.

So application memory management and storage will be saved.





This is the section we can immutable easily with only alphabet letters. So that user selects the letter or enters the letter.

So application memory management and storage will be saved.



This is the section we can immutable easily with only 0 to 9 letters and some important symbols using for the house numbers. So that user selects the number or symbol or enters the letter.

So application memory management and storage will be saved.

With now above examples, we can find easily how benefits of an immutable type.

All fundamental data types like int type, float type, complex type, bool type, str type all are immutable. But object reusable only int type, float type, bool type, and str type. Complex type objects reusable, not applicable.

IDLE (Integrated Development and Learning Environment) and python console it looks like (>>>) are REPL (Read, Evaluate, Print, Loop) tools.

Mutable (changeable):

Now we are taking one example (number = 10 and list =[10,20,30,40]) now number contain only one value, the list contains multiple values. And we can access the list values through index like list[0], list[1] this method.

All fundamental data types are immutable. Once create the object we can’t perform any changes, if we are trying to change then it will create the new object.

But the list is mutable, for example

list= [10,20,30] now we are check id or address in this list.

list= [10,20,30]

print(id(list))

24768200  #result like this.

Now we are trying to change list[0]=89 and print list and id also.

list= [10,20,30]

list [1]=89

print (list)

print(id(list))

[10, 89, 30] #result like this.

24768200

Before list value is [10,20,30] and we are trying to change in place of index[1] or 20 replace with 89, it’s changed now list value is [10,89,30] but id or address not changed. This type of nature is called mutability.

One more example

list = [10,20,30]

list_1 = list

list[2] = 56 # we are trying to change the list value of 30 replace it with 56.

Now list and list_1 id’s and values are the same.

If we are trying to change the one index value then automatically list_1 value also changed. But id’s are the same.

Ex: 

list= [10,20,30]

list_1 = list

list [2]=89

print (list)

print (list_1)

print(id(list))

print(id(list_1))

[10, 20, 89]    #result like this.

[10, 20, 89]

16838344

16838344

 

 


0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post