
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pass by Reference vs Value in Python
In Python Call by Value and Call by Reference are two types of generic methods to pass parameters to a function. In the Call-by-value method, the original value cannot be changed, whereas in Call-by-reference, the original value can bechanged.
Call by Value in Python
When we pass an argument to a function, it is stored locally (in the stack memory), i.e the scope of these variables lies with in the function and these will not have effect the values of the golbal variables (variables outside function).
In Python, "passing by value" is possible only with the immutable types such as integers, floats, strings, and tuples. Even though we pass these as a reference, the object itself cannot be changed.
Example
In the below example code x is an integer (immutable). The num was changed inside the function, but x remains unaffected outside the function.
def modify_number(num): print(f"Original: {num}") num += 10 print(f"Modified inside function: {num}") x = 5 modify_number(x) print(f"Outside function: {x}")
Output
Original: 5 Modified inside function: 15 Outside function: 5
Call by Reference in Python
Unlike in "Call by Value", When we call a method by "passing the reference of a value (object)", the original value will be modified
The formal and actual arguments are in the same address space, Since we are passing the reference address of the arguments, if we modify a local variable, it is reflected throughout the function.
In Python, we can only pass the reference of mutable types such as lists, dictionaries, and sets, to a function.
Example
In the below example, we are passing the reference of the list object to a function. Since a list is a mutable object, the modifications made inside the function are reflected in the original.
def modify_list(my_list): print(f"Original: {my_list}") my_list.append(4) print(f"Modified inside function: {my_list}") my_list = [1, 2, 3] modify_list(my_list) print(f"Outside function: {my_list}")
Outside
Original: [1, 2, 3] Modified inside function: [1, 2, 3, 4] Outside function: [1, 2, 3, 4]
Pass by reference vs value
Some of the key differences between passing a parameter by its value and by its reference are as follows.
Feature | Pass by Value | Pass by Reference |
---|---|---|
Objects | Immutable types (integers, strings) | Mutable types (lists, dictionaries) |
Effect of modification | Changes inside the function do not affect the original object outside the function. | Changes inside the function directly affect the original object outside the function. |
Behaviour | It creates a new copy of the object for the function. | The function works with the original object reference. |
Use Case | Only for the types where the values should not change. | Only for the types where in-place modification is needed. |