Immutable And Mutable Datatypes in Python | Python Notes | Digital Community



Variables in Python


In this post, we are going to discuss variables and Memory Management in Python but before these, if You are not seen our video on this topic please see it first, then you will understand the concepts very well.



 



In this post we are going to see :

 

1.     How Variables In Python Are Different Than Other Languages?

 

2.   Immutable And Mutable

 

3.   The id( ) Function and The is Operator

 

So let’s start.

 

1. How  Variables In Python Are Different Than Other Languages?

 

Suppose we have written :

 

a = 98

 

by writing the above statement Python has created two things :

 

1. It created an object in Heap Memory and stored 98 in it and after that,

 

 

2. He created a reference of name ‘ a ‘ which is pointing to this object whose value is 98.

 

So Let’s talk first about Heap Memory :

 

Heap Memory is that part of the Memory which is created Dynamically allocated when objects are being created.

 

So, when we run a program and during run-time when objects are created, and where these objects are stored, we call the memory as Heap memory.

 

And these objects remain in the heap memory as long as:-

 

i. we remove them or

 

ii. Until our program terminates.

 

We can Understand it by diagram :





A reference is a name that refers to the unique location in memory of a value (object).

 

After that, we have created one more reference of name ‘ b ‘, and we have assigned the address of previously created object by writing :

 

b=98

 

By doing that, now the references ‘ a ‘ and ‘ b ‘, start pointing to the same object created by us earlier.

 

Let’s understand it well by the following diagram :




And it becomes more clear in the notes of the id() (function) or is (keyword) which is mentioned below.


Python Memory Management

 

 

2. Immutable And Mutable :

 

Now, if we assign a new value to our ‘ a ‘ reference, then it will not overwrite the value of its object, rather it will create a new object with the new value provide that is 93 in our case.

 

So let's understand this from the diagram as well :






And, this behavior is called Immutability in Python.

 

So in Python, when we cannot change the value inside an object, then we call it Immutable. And if we are able to change the inside value of an object then we call it Mutable.

 

So, objects of the following data types are categorized as Immutable :

 

·      int

 

·      float

 

·      bool

 

·      str

 

·      tuple

 

·      complex

 

·      range

 

·      frozenset

 

·      bytes

 

 

On the other hand objects of the following datatypes are considered as Mutable in nature :

 

 

·      list

 

·      dict

 

·      set

 

·      bytearray

 

 

After knowing the concepts of Immutability, generally, two question arises on a programmer’s mind :

 

      1. What is the benefit of making objects immutable?

 

      2. If we have a single reference and we create multiple objects, then wouldn’t there be memory wastage?

 

So firstly, we will talk about the Benefits of Immutability.

 

The main Benefit of Immutability is that, it prevents the unnecessary creation of new objects.

 

      This is because if we write, the following 2 statements:

 

               a=98

 

               b=98

 

      Then Python will not create 2 objects. Rather it only creates 1 object and makes both the references a and b, refer to the same object.

 

      This saves memory and overhead  of creating multiple objects

 

The answer of the Second question you will found on the link given below :

 

 

 

3. The id( ) Function and The is operator

 

 

So, we have just told that python will do the above things when we create an object in Python. But where is the Proof?

 

So, to prove it that Python will do the same thing as we have illustrated in our diagram, Python has provided us two ways :

 

The first one is we can either use the Built-in function provided by Python that is the id() or we can use the is (keyword/operator)  provided by python so firstly let’s see the id() function :

 

The id() function in Python will give the address of that object whose reference is provided to it as an argument.

 

Let’s see an Example

 

Suppose we have write :

 

a=98

b=98

id(a)

id(b)

 

Then, Python will give the following results :

>>> a=98

>>> b=98

>>> id(a)

4548032192

>>> id(b)

4548032192


If we observe it then we will find that the address of both the references is the same, so we can say that Python has created a single object and assign its address to both the references that are ‘ a ’ and ‘ b ‘. 

 

Another way to find out whether 2 references are pointing to the same object or not is to use the is operator.

 

The is operator is a binary operator and checks whether both the operands refer to the same object or not.

 

 

Suppose we write :

 

a=98

b=98

print(a is b)

 

Then, Python will give the following results :

>>> a=98

>>> b=98

>>> a is b

True

>>> c='Digital'

>>> d='Digital'

>>> c is d

True


Hence, we can say that our above explanation is correct.

 

But there is an Important Twist in Python :

 

Suppose we write :

 

a=-6

b=-6

a is b

 

Then, Python will show the following output :

>>> a=-6

>>> b=-6

>>> a is b

False


But, if we write :

 

a=-1

b=-1

a is b

 

 

Then, Python will show the following output :

>>> a=-1

>>> b=-1

>>> a is b

True 


So, it happens because in research it is found that 90% of the time the number used by a programmer lies in the range of ( -5  to  254 ).

 

That’s why Python does not create new objects for numbers within this range each time.

 

Let’s see an Example :


>>> a=257

>>> b=257

>>> a is b

False

>>> a=256

>>> b=256

>>> a is b

True

 

But if we use any number outside this range, then Python will create a new object for all of them.

 

Let’s Understand it by an example :

 

Suppose we write:

 

a=1.0

b=1.0

a is b

 

After that we wrote :

 

a=3+4j

b=3+4j

a is b

 

Then, Python will give the following Output :


>>> a=1.0

>>> b=1.0

>>> a is b

False

>>> a=3+4j

>>> b=3+4j

>>> a is b

False

This happened because even though the float and the complexes come in the Immutable datatypes. But they do not follow the caching and concept of reusability.

 

Means whenever we write any new statement related to these datatypes then, Python will automatically create new objects instead of changing the value of the previous object.

 

 

So, I hope you will understand that how variables/references work in Python and Immutability and Mutability behavior of Datatypes in Python.

 

And still, you have any doubts about the above mention topics then you will ask your doubt in the comment section. I will try to sort it out as soon as possible.

 

 

DIGITAL COMMUNITY

 

       We Make IT Happen.

No comments:

Post a Comment

If you have any doubts, let me know.