Is Python pass by reference or pass by value? This question intrigues every Python programmer coming from a C or Java background. In this tutorial, you will get its answer and learn the meaning of “pass by reference” and “pass by value” along with their difference in Python.
Python Pass by Reference vs Pass by Value
These are two important concepts for understanding how functions handle variables and data in Python. This guide will break them down for you in simple terms.
Variables in Python: Not the Actual Object
Before exploring Pass by reference and Pass by value, understand that variables in Python are not the objects themselves. Instead, they act as “labels” or “pointers” to the objects in memory. When you pass a variable to a function, you pass a reference to the object, not the variable itself.
Think of a variable as a bucket. Inside the bucket is the object. When you pass the bucket (variable) to a function, you pass access to what’s inside. How the function uses that access depends on whether the object is mutable or immutable.
Pass by Reference vs Value in General
This is how these concepts work in general programming.
Pass by value means a copy of the data is passed to the function. This means any changes made inside the function do not affect the original value outside.
Pass by reference implies that the actual data (or a reference to the original data) is passed. Changes made inside the function will affect the original value outside.
Pass by Reference vs Value in Python
In Python, things work a bit differently. Python uses a method called “Pass by Object Reference”. Depending on the type of object you pass (mutable or immutable), Python behaves either like “pass by value” or “pass by reference.”
Mutable vs Immutable
Understanding the difference between mutable and immutable data types In Python is key:
Immutable types are those whose values cannot be changed once created. They behave like “pass by value” because changes inside the function don’t affect the original entity. Examples include integers, strings, and tuples.
Mutable types are those which you can modify anytime. They behave like “pass by reference” because changes inside the function affect the original entity. Examples are lists, dictionaries, and sets.
Python Pass by Reference
“Pass by reference in Python” means when you pass a variable to a function, you pass a reference to the object it points to, not a copy of the self. Changes made inside the function affect the original entity. This concept is also known as “Pass by object reference”.
To understand “how to pass by reference in Python”, look at this example using lists, which are mutable types:
def modify_list(in_list):
in_list.append("Beamers")
print("Inside fun():", in_list)
in_list = ['For', 'Tech']
modify_list(in_list)
print("Outside fun():", in_list)
# Output
# Inside fun(): ['For', 'Tech', 'Beamers']
# Outside fun(): ['For', 'Tech', 'Beamers']
In this example, we passed a list to the modify_list(). Since lists are mutable, the function adds an item to the list, and the change is visible inside and outside the function.
Python Pass by Value
“Pass by value in Python” refers to passing a copy of the variable’s value to the function. Changes to this copy inside the Python function do not affect the original value.
Consider this example with integers, which are immutable:
def modify_num(n):
n = n + 10
print("Inside fun():", n)
n = 5
print("Before fun() call:", n)
modify_num(n)
print("After fun() call:", n)
# Output
# Before fun() call: 5
# Inside fun(): 15
# After fun() call: 5
In this case, we passed an int value to the modify_int (). Since they are immutable, the function created a new value but did not change the original one.
Important Differences
Let’s summarize the differences one by one, though you might have already understood.
Python Pass by Reference means to pass the reference to the original object. Changes to the object inside the function affect the original object. Like lists and dictionaries, Python passes the reference. Changes to these objects inside the function will affect the original object.
Python Pass by Value means to pass a copy of the value. Changes to this copy inside the function do not affect the original value. Like integers and strings, Python passes a copy. Hence, any changes inside the function do not affect the original value.
Frequently Asked Questions
By now, you most likely understand the difference between pass-by-reference and pass-by-value in Python. However, let’s take a look at some common questions that you may still have:
Q-1 Is Python pass by reference or pass by value?
Answer: Python uses pass-by object reference. This means it behaves like a pass-by-value for immutable objects and pass-by-reference for mutable ones.
Q-2 Can you modify an immutable type inside a function in Python?
Answer: No, you cannot modify an immutable type like an integer or string inside a function. If you try to change it, Python will create a new object in memory, leaving the original one unchanged.
Q-3 How does Python handle lists in functions?
Answer: Lists are mutable types, so when you pass a list to a function, any changes made inside the function will affect the original list outside the function.
Q-4 Does reassigning a variable inside a fun() change the original object?
Many developers think Python works purely by pass-by-reference, but that’s not always the case. For example – reassigning an object won’t change it even if it is mutable. It will only change the local copy of the reference.
def reassign_list(in_list):
in_list = ["new list"] # This reassigns the local var
print("Inside fun() - list:", in_list)
nums = [1, 2, 3]
reassign_list(numbers)
print("Outside fun() - list:", nums)
# Output
# Inside fun() - list: ['new list']
# Outside fun() - list: [1, 2, 3]
Related Topic – 100 Python Interview Questions and Answers PDF
Take the Quiz
1. What is the main difference between Python pass-by-reference and pass-by-value?
A. Pass-by value sends a copy; pass-by reference sends the original.
B. Pass-by value sends the original; pass-by reference sends a copy.
C. Both are the same; changes affect the source.
D. They are unrelated.
Show Answer
Answer: A
2. What happens if you pass a list to a fun() in Python and modify it inside?
A. The source list remains as is; changes are local to the fun().
B. The fun() creates a new list, leaving the source list unchanged.
C. The source list is modified because lists are mutable.
D. The fun() raises an error as lists cannot be modified.
Show Answer
Answer: C
3. What kind of data types are considered immutable in Python?
A. Lists and dictionaries
B. Numbers, strings, and tuples
C. Sets and lists
D. Dictionaries and sets
Show Answer
Answer: B
4. How come the change in an integer argument is not reflected outside a fun() in Python?
A. Integers are mutable; changes affect the original.
B. The fun() creates a new integer without affecting the original one.
C. Python passes integers as pass-by reference leaving the original as is.
D. Integers are immutable, so the original value stays as is.
Show Answer
Answer: D
5. What happens when you reassign a mutable object inside a fun()?
A. The original object is modified.
B. The local variable inside the fun() is updated, but the original object remains as is.
C. The fun() creates a new object, and the original one is deleted.
D. The fun() raises an error as reassigning mutable objects is not allowed.
Show Answer
Answer: B
Before You Leave
Hopefully, the above explanation has given you the answers, mainly whether Python passes arguments by reference or value. However, we have tried to cover many other related concepts like mutable vs immutable types.
Finally, to keep our site free, we need your support. If you found this tutorial helpful, please share it and subscribe to our Linkedin or YouTube.
Happy Learning,
TechBeamers.