Mutable and Immutable Types
The Python data objects can be broadly categorized into two - mutable and immutable types, in simple words changeable or modifiable and non-modifiable types.
The Immutable types are those that can never change their value in place. In Python, the following types are immutable: integers, floating-point numbers, Booleans, strings, tuples.
Let us understand the concept of immutable types. To understand this, consider the code below :
sample code 5.1
p = 5
q = p
r = 5
: #will give 5, 5, 5
p = 10
r = 7
q= r
After reading the above code, you can say that values of integer variables p, q, r could be changed effortlessly. Since p, q, r are integer types, you may think that integer types can change values.
But Hold: It is not the case. Let's see how
You already know that in Python, Variable-names are just the references to value-objects i.e., data values. The variable-names
do not share values themselves i.e., they are not storage containers.
Now consider the sample code 5.1 given above. So although it appears that the value of variables p/q/r is changing; values are not changing "in place" the fact is that the variable names are instead made to refer to new immutable integer objects. (Changing in place modifying the same value in same memory location).
- Initially these three statements are executed :
q = p
r = 5
You can check/confirm it yourself using id( ). The id( ) returns the memory address to which a variable is referencing.
In [40] : p = 5
In [41] : q = p
In [42] : r = 5
In [43] : id(5)
Out[43] : 1457662208
In [44] : id(q)
Out[44] : 1457662208
In [45] : id(r)
Out[45] : 1457662208
In the above example you can see id( ) is returning the same memory address for value 5, p, q, r - which means all these are referencing the same object.
Please note memory addresses depending on your operating system and will vary in different sessions.
- when the next set of statements execute,i.e.
r = 7
q = r
then these variable names are made to point to different integer objects. That is, now their memory addresses that their reference will change. The original memory addresses of p that was having value 5 will be the same with the same value i.e., 5 but p will no longer reference it. The same is for other variables.
Let us check their ids
In[47] : p=10
In[48] : r=7
In[49] : q=r
In[50] : id(10)
Out[50] :1457662288
In[51] : id(p)
Out[51] : 1457662288
In[52] : id(7)
Out[52] : 1457662240
In[53] : id(q)
Out[53] : 1457662240
In[54] : id(r)
Out[54] : 1457662240
In[55] : id(5)
Out[54] : 1457662208
Now if you assign 5 to any other variable. Let us see what happens.
In[56] : t = 5
In[57] : id(t)
Out[57] : 1457662208
Thus, it is clear that variable names are stored as references to a value object. Each time you change the value, the variable's reference memory address changes.
Variables (of certain types) are NOT LIKE storage containers i.e., with fixed memory address where the value changes every time. Hence they are IMMUTABLE.
The objects of the following value types are immutable in Python :
- Integers
- Floating point numbers
- Booleans
- Strings
- Tuples
Mutable Types
The mutable types are those, where values can be changed in place. Only three types are mutable in Python. These are :
- Lists
- Dictionaries
- Sets
Chk = [2, 4, 6]
Chk[1] = 40
It will make, the list namely Chk as [2, 40, 6].
In[60] : Chk = [2, 4, 6]
In[61] : id(Chk)
Out[61] : 150195536
In[62] : Chk[1] = 40
In[63] : id(Chk)
Out[63] : 150195536
Lists and dictionaries shall be covered in the later posts.
So with this, we end with this Topic, don't forget to follow me so that you can get the latest updates of my new posts that I upload weekly, I will try my best to upload daily OR you can Sign up for Newsletters at the top of the Blog and that will give you E-mail updates of my New Post.
Jai Hind!!!!
Vande Mataram!!!
No comments:
Post a Comment
If you have any doubts, please do let me Know.