About Me

My photo
Mumbai, Maharastra, India
He has more than 7.6 years of experience in the software development. He has spent most of the times in web/desktop application development. He has sound knowledge in various database concepts. You can reach him at viki.keshari@gmail.com https://www.linkedin.com/in/vikrammahapatra/ https://twitter.com/VikramMahapatra http://www.facebook.com/viki.keshari

Search This Blog

Sunday, September 1, 2019

How immutable objects are treated in python function

ImMutable: An immutable object can’t not be changed after it is created, example are: any build-in datatype-int, float, str, bool, tuple)

What exactly it means that, when you declare and initialize any immutable variable, it preserve a space in the memory, and the moment you change the value of the variable, modified value get a new  space in memory.

>>> a=3
>>> a
3
>>> id(a)
1812388048
>>> a=a+2
>>> a
5
>>> id(a)
1812388080
>>> type(a)
<class 'int'>

Here in above example if you see, Variable “a” is initialized with value 3 and it occupies memory address of 1811288048, and the moment we tried to update the value of Variable ‘a’ to 5, the modified value i.e. 5 gets a  new memory space which is 1812388080, that is because of immutable characteristics of int datatype.

Now lets create a function, here the function takes a parameter of int type

def updateNumber(n):
   
print(id(n))
    n+=
10
   
print(n)

lets try to call the function with a int variable

def updateNumber(n):
   
print('Inside function, before modifying value of a, Mem address of n = ',id(n))
    n+=
10
   
print('Value of a inside function after modification =', n)

a=
5
print('Before calling function Mem address of a = ', id(a))
updateNumber(a)
print('Value of a after calling function = ', a)

Output:

Before calling function Mem address of a =  1812388080
Inside function, before modifying value of a, Mem address of n =  1812388080
Value of a inside function after modification = 15
Value of a after calling function =  5

Here if you see, the location of n inside the function is same as location of a, before calling function i.e. 1812388080, then function modify the value of n from 5 to 15 so inside function when we tried to print the value of n, it gave 15. But once we came out of function and tried to print the value of a, it still gave 5 unchanged.

That means modification of value inside function was not retained outside function. Lets find out why .. by printing a location of n after modifying the value of n in function.

def updateNumber(n):
   
print('Inside function, before modifying value of a, Mem address of a = ',id(n))
    n+=
10
   
print('Inside function after modification of value of n, location of n is = ', id(n))
   
print('Value of a inside function after modification =', n)

a=
5
print('Before calling function Mem address of a = ', id(a))
updateNumber(a)
print('Value of a after calling function = ', a)
print('After calling function Mem address of a = ', id(a))

Output:

Before calling function Mem address of a =  1812388080
Inside function, before modifying value of a, Mem address of a =  1812388080
Inside function after modification of value of n, location of n is =  1812388240
Value of a inside function after modification = 15
Value of a after calling function =  5
After calling function Mem address of a =  1812388080

Here we can see, the location of n inside function was same as a outside function but the moment function modifies the value of n, it took a separate location and that is the reason why a value even after modifying it inside function didn’t change.

The reason behind this kind of behavior is datatype int is immutable.


Data Science with…Python J
Post Reference: Vikram Aristocratic Elfin Share

No comments:

Post a Comment