Friday, 13 September 2013

PYTHON-------> OVERVIEW OF THE KEY CONCEPTS

Reference semantics

Everything in Python is an object and Python passes references to those objects i.e. consider the following case
 >>> a=[1,2,3,"hello",4,"hai"]
>>> b=a

while this code is interpreted this is what internally happens.

 a--------------->[1,2,3,"hello",4,"hai"]
                                ^
                                |
                                |
                                b
Internally in the memory there is only one list "[1,2,3,"hello",4,"hai"]" and both "a" and "b" are mere names with which you can acees the memory location.
this can be demonstrated by the following set of code.
>>> a=[1,2,3,"hello",4,"hai"]
>>> b=a
>>> a.append("change")
>>> print b
[1, 2, 3, 'hello', 4, 'hai', 'change']

The change made to "a" internally reflects to "b" this makes it clear that "a" and "b" refers to the same thing.
If we pass an immutable object to a method, it references the original object, and you can't change the object.

Memory management

 In C language we use malloc() to allocate memory and free() to free the allocated memory.IF we forget to free() the memory that has been allocated serious problems may occur and there are chances for your program to get "crashed".
Python introduces a solution for this by "automatic memory management". Python uses reference counting to detect inaccessible objects. The "garbage collection" module provides functions to force garbage collection.It uses something called "ref-count" to do this. 

Reference counting and garbage collection

Python automatically deletes inaccessible memory locations. For example consider the following set of code.
 >>> a=[1,2,3,"hello",4,"hai"[ref_count=1]] #ref_coutn is a hypothetical concept and not a member of the list]
>>> b=a
>>> a.append("change")
>>> print b
[1, 2, 3, 'hello', 4, 'hai', 'change'[ref_count=2]]
>>> c=b
>>> c=(1,2,3)
>>> b="steve"
>>> a="john"
here this is what happens
step1
-----------------------------------------------------------------------
a--------------->[1,2,3,"hello",4,"hai"]
                                ^ 

                                |

                                |

                                b
a--------------->[1,2,3,"hello",4,"hai","change"[ref_count=2]]   
                                ^ 

                                |

                                |

                                b
 step2
----------------------------------------------------------------------------------
a--------------->[1,2,3,"hello",4,"hai","change"[ref_count=3]]
                                ^  ^

                                |   |

                                |   |

                                b  c
step3
---------------------------------------------------------------------------------
a--------------->[1,2,3,"hello",4,"hai","change"[ref_count=2]]
                                ^ 

                                |

                                |

                                b                c----->(1,2,3)
step4
-----------------------------------------------------------------------------------

a--------------->[1,2,3,"hello",4,"hai","change"[ref_count=1]]
                              
b------>"steve"
c------->(1,2,3)

step5
------------------------------------------------------------------------------------

""----------------------->>>[1,2,3,"hello",4,"hai","change"[ref_count=0]] 
 a------>"john"
 b------>"steve"
c-------->(1,2,3)

************************************************************
Now it is evident that there is no name with which we can access th list
[1,2,3,"hello",4,"hai","change"[ref_count=0]]
and the ref_count is 0 hence python gets to know that it is a mere waste of memory therefore python automatically releases that memory space.





0 comments:

Post a Comment