Instant Search

Python is call by object.

There are two ways to pass arguments/parameters to function calls in c programming:

  • Call by value
  • call by reference.

    Call by value:

In call by value, original value is not modified. In call by value, value being passed to the function is locally stored by the function parameter in stack memory location. If you change the value of function parameter, it is changed for the current function only. It will not change the value of variable inside the caller method such as main(). call by value a copy of actual arguments is passed to respective formal arguments.

 

        Call by reference :

 

In call by reference the location (address) of actual arguments is passed to formal arguments, hence any change made to formal arguments will also reflect in actual arguments.

In call by reference, original value is modified because we pass reference (address).

Here, address of the value is passed in the function, so actual and formal arguments shares the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function.

In simple words in call by value the function store & copy temporary its value from its main memory to  in stack memory so if program did something with values it will effect value only inside of the program after the program exist the original value take place.

while in call by reference the function pass the memory address of its original value so it means its passing directly original value so if program did something with value it will directly effect original program.

Call By Reference

Call By Value

In Pass By Reference method, the Address of the Variables are passed as arguments to the Function.

In Pass By Reference Method, the Values of the Variables are passed as arguments to the Function.

There is no copy created on the Memory Stack.

A Copy of the Original Variables are created in Memory Stack.

The modifications are made to the Original Variables (Old Values).

The modifications are made to the New Variables.

The changes are visible in every Function.

The changes are visible only in a particular Method.

Pointers are used in Call By Reference approach.

No Pointers are used in Call By Value Approach.

Pass By Reference method creates only single copy thereby providing Memory Efficiency.

Pass By Value approach creates Two or more copies of the Original Variables and hence it is not memory efficient.

The Actual and Formal Parameters are created on the same Memory Stack.

The Actual and Formal Parameters are created on different Memory Stack.

main-qimg-197a0c5aa96302b99b8c1d1178d8a86a

 

call by value pass the copy of value not origional value , it means it remains uchange if we effect or modify the vaule in function then origional value will not change.

call by reference pass the reference of variable

 

Enough about call by value and call by reference but python don’t use anyone of them.  Python uses a mechanism, which is known as “Call-by-Object”, sometimes also called “Call by Object Reference” or “Call by Sharing”.

 

Python use call by object:

If you have read my previous post where i briefly described “object” and “object reference” in python. If you have not read i suggest first read this “Everything is object in python”  article.  I already explained that a variable is in python is not holding a value its just name. Like a person have name , body and soul. So person can have many names but he must have only and only one body and one soul. Person name is “variable” , person soul is “value” and person body is “object” which is holding the value. so if a person have many names like tim , john , exe then it will not effect value , name are just referring the person body (object).

so if i say:

it means i is a variable which is not holding the value of 22 which is just referring the object and object is holding the value 22. so variable i is just pointing the object.

so if variable are object then when we pass values in argument in function it means we are not passing values or references of values  we are passing variable name and variable name are referring those objects which are holding those values.

here we are passing hi as argument in function calling and hi is not holding the value “22” instead of  we are passing a variable hi  which is pointing an object who is holding the value 22. so we are passing object reference in function calling. “hi” is just variable reference so when we pass hi as argument it means we are passing object reference of that object who is holding the value 22.

Untitled

Variables in Python aren’t values, they’re object references. When you call a Python function the arguments are copies of the references to the original object.

 

a is assigned to the object 1, and b is assigned to the list object containing a reference to the object 2. Inside of the function foo, bar is also assigned to the same object 1 and baz is assigned to the same list object. Since 1 is immutable you can’t change the object, but you can reassign bar to refer to a different object such as 3. A list is modifiable, so by setting baz[0] to 4 you are also changing the list object that b refers to. The output of the above will be 1 [4].

python have dynamic nature about data types:

first i was pointing a object who is holding value 23

firsto

when we change i=”exe” it means we have not effected 23 we just change the pointing to new object.

secondo

which is “exe” when we change i=[] it doesn’t mean we affected the value “exe” exe and 23 is  they may live on indefinitely.

thirdo

 

 

 

same when we change i=45.99 then it doesn’t mean we affected previous values it means we just referring another object now and those previous values are no effected.

five0

if you don’t believe that 23, “exe” and [] is still not effect even now i have i=45.99 then look at this:

Now let see the output :

As you can see int and new int have same id what it means it means when we assign i=”exe” then int 23 was always there it was not destroyed when we assign new variable to access that old object it immediately take old place and that’s why id is same.

same string and new string have same id it means string “exe” was never destroyed when we assign i=[]

same with list when we replace i=45.99 then list i=[] was always there it was not destroyed.

now float have same value it means an object can have many variables name but it will point only object. “i” and “l” are just name which are pointing one object 45.99

Now some objects are mutable and some are immutable which i have described in briefly in previous article here “”Everything is object in python”   so

Mutable and immutable data types:

The following are immutable objects:

  • Numeric types: int, float, complex
  • string
  • tuple
  • frozen set
  • bytes

The following objects are mutable:

  • list
  • dict
  • set
  • byte arrayso when we pass mutable type data as reference in argument then python act like pass by reference it means if function affect the value inside the function then original value also effected.but when we pass the immutable data types it act as pass by value it means if function affect the value inside the function then original value will not be affected.read my previous post i have described this point briefly.we can understand by example :when we pass mutable data types:

output:

when we pass the list it modify the original value even after the function exist.  but id are same it means object is not change but its value is content is changed.

although we can prevent this by passing copy of list instead of original list :

output :

You can see now original value of list is not effected because we pass the copy of list by [:] , ho and list variable pointing the different object they are not same.

now let’s test on immutable object:

output:

So as you can see object value and id doesn’t change even we modify the object value inside function after when function exist the old value take place.

#extra

if you try this program:

it will give error :

You understand the reason for this error if you read my previous post “Namespace , scope and LEGB rule”
r
eason is “int” variable is global variable so we can use it in the program because when we use “int” in program it fund “int” value first in local namespace and then in global namespace and it will find “int” value in global namespace . but if we give print statement and after assign new value to “int” variable then since we have assign and declared “int” in local namespace so now when it will start trying to search the value it always start from local namespace –> enclosed namespace –>global namespace –>built-in namespace   so if it find int declaration in local namespace it will ignore global namespace and will use local namespace value but as we print first before assigning “int” a new value in local so that’s why it will give error “local variable ‘int’ referenced before assignment”.

I think now you got very good idea about how “Call by object works” in python.

Leave a Reply

Your email address will not be published. Required fields are marked *