Python Mutable vs. Non-Mutable Data Types

In this article, you will learn about python mutable vs. non-mutable date type and how they affect passing of arguments to the function.

Advertisements

In the previous article, you learned about passing arguments by reference and passing arguments using values. The choice of passing arguments depends on the data type.

Mutability is the ability of variable to change its value after being declared. A mutable variable can change its value after being declared. The data type of mutable give this ability.

An immutable variable is that which cannot change its value after being declared. The data type of such variable does permit mutability. Python basic data types are mostly immutable types.

Variable Declaration

A variable declaration is the process of assigning a value to the variable. This process reserves a memory location for the variable to store its data.

For example.

myInt  = 44
print(id(myInt))
myList = ["Apple","Orange","Grapes"]
print(id(myList))

The code above will create two memory location and store its values. The first value is an integer and second one is a list type.

The program uses id() function to display the memory addresses of the variables. Output is given below.

140713857296112
321864522440

Now, if we try to change the value of integer which is a mutable variable because of integer type. It won’t update the existing variable, but create a new variable at a new memory location.

myInt = 44
print("Before: ",id(myInt))
myInt = myInt + 10
print("After: ",id(myInt))

The result of myInt = myInt + 10 is stored in new location. Here is the output.

Before:  140713857296112
After:  140713857296432

This is because integer is immutable type.

Python Immutable Type

Consider same example with a string.

myString = "mycar"
print(myString)
print("Before: ",id(myString))
print(myString.upper())
print("After: ",id(myString))

In the example above, the program changes the original text lowercase letters to uppercase letters using python upper() function.

Advertisements

After changing the string to uppercase, it does not change the value or memory address of the original text.This is because python string type is also immutable. Here is the output.

mycar
Before:  321865256432
MYCAR
After:  321865256432

Immutable Type with Function

An immutable variable when passed as parameter will act list it is passed by value because it will not change the original value passed variable.

Passed by Value means variable is immutable.
Passed by Value means variable is immutable.

Python Mutable Type

There are newer data types in python that allows to change the original variable data.

myList = ["Apple","Orange","Grapes"]
print(id(myList))

The above is a list data type which is mutable. That means you can change it without re-declaring it in a new location.

myList = ["Apple","Orange","Grapes"]
# printing the memory address of the list
print(myList)
print(id(myList))
# adding banana to the list 
myList.append("Banana")
# print the memory address of the updated list
print(myList)
print(id(myList))

Once we run the program, the program adds a new item to the list. We also print the original list with the updated list.

Here output is given below.

['Apple', 'Orange', 'Grapes']
321864522760
['Apple', 'Orange', 'Grapes', 'Banana']
321864522760

The list is updated and the new item appears in the list. However, you note that the memory address of the myList has not changed. That is because the list data type is mutable.

Mutable Variable and Function

A mutable variable when passed as parameter to the function will compute and change the original value of the variable. It works similar to passing by reference to a function.

Passing by reference changes original value because it is mutable
Passing by reference changes original value because it is mutable

Re-Declaration

Whenever we re-declare a mutable variable, it will be created in a new location. However, immutable variable is value based, therefore, when you create a new variable and assign it some value. If the value is already available in the memory. Python assign that value to new variable.

Suppose myInt has a value of 23 stored at 74384844. If you re-declare myInt than it is going to point to same memory location 74384844 where 23 is available.

See the example below.

print("Before")
myInt = 23 
myStr = "Hello"
myList = [1, 3 , 6, 9]
print(id(myInt))
print(id(myStr))
print(id(myList))
# re-declare the variable with same value
print("After")
myInt = 23 
myStr = "Hello"
myList = [1, 3 , 6, 9]
print(id(myInt))
print(id(myStr))
print(id(myList))

The output of the program is given below.

Before
140713857295440
321852102896
321864540808
After
140713857295440
321852102896
321852148936

The value of immutable type is does not change memory location because it is assigning the available value 23 and “Hello” in the memory. The value of mutable variable myList changes due to re-declaration.

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.