Python utilizes a system, which is known as “Call by Object Reference” or “Call by assignment”. In the event that you pass arguments like whole numbers, strings or tuples to a function, the passing is like call-by-value because you can not change the value of the immutable objects being passed to the function. Whereas passing mutable objects can be considered as call by reference because when their values are changed inside the function, then it will also be reflected outside the function.
Inside Function: GeeksforGeeks Outside Function: Geeks
Inside Function [10, 20, 30, 40, 50] Outside Function: [10, 20, 30, 40, 50]
Binding Names to Objects
In python, each variable to which we assign a value/container is treated as an object. When we are assigning a value to a variable, we are actually binding a name to an object.
110001234557894 110001234557894 True
Now, let’s try and understand this better with another example.
541190289536222 541190288737777 False
The output of the above two examples are different because the list is mutable and the string is immutable. An immutable variable cannot be changed once created. If we wish to change an immutable variable, such as a string, we must create a new instance and bind the variable to the new instance. Whereas, mutable variable can be changed in place.
Inside Function: new value Outside Function: old value
In the above example, a string which is an immutable type of object is passed as argument to the function
foo. Within the scope of the given function
a= "new value" has been bounded to the same object that
string has been bound outside. Within the scope of the function
foo, we modify “old value”` to “new value”. Once we leave the scope of function
a="new value" is no longer in the name space, and the value that string refers to was never changed.
Example 4: Now, let us look at how mutable variable is passed into the function.
['Nothing, 'how', 'are', 'you', 'doing']
When we pass a
mutable variable into the function
foo and modify it to some other name the function
foo still points to that object and continue to point to that object during its execution.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Pass by reference vs value in Python
- Shared Reference in Python
- How to Call a C function in Python
- Python - Call function from another function
- call() decorator in Python
- Python: Call Parent class method
- Python - Call function from another file
- Decorator to print Function call details in Python
- How to call C / C++ from Python?
- PyQt5 QCalendarWidget - Removing memory reference
- Pafy - Getting Weak Reference of Stream
- Python - Extract ith Key's Value of K's Maximum value dictionary
- Python | Sort Python Dictionaries by Key or Value
- What is the maximum possible value of an integer in Python ?
- Python | Extract key-value of dictionary in variables
- Python | Check if all the values in a list that are greater than a given value
- Python Regex to extract maximum numeric value from a string
- Python | Check if all the values in a list are less than a given value
- Python | Ways to Spilt the list by some value
- Python | Select random value from a list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.