Pass by Reference vs Pass by Value in Python

Meenakshi Agarwal
By
Meenakshi Agarwal
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large...
10 Min Read

Is Python pass by reference or pass by value? This question is a puzzle for many programmers coming from a C or Java background. In this tutorial, we have explained pass by reference and pass by value separately and then covered their differences.

Understanding Python Pass by Reference and Pass by Value

Pass by reference and Pass by value concepts are related to variables in Python. So, it is essential that you know a variable is not an object itself. Instead, it works as a “label” or “pointer” holding the object in memory. When you pass a variable to a function, it actually holds the reference to the object, not just the value.

To simplify this, take an example where you assume a variable as a bucket that has the object inside. When you pass the bucket to a function, it gets the access to what’s inside the variable. How the function uses that access depends on whether the object is mutable or immutable.

Pass by Reference vs Pass by 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.

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.

Difference Between Python Pass by Reference and Pass by Value

Let’s summarize the main difference of these two popular programming concepts.

Pass by reference in Python mean when you give a variable to function, function no get new copy but get reference to same object. This happen with mutable types like list and dictionary. If function change object, change also happen to original object outside function. This because both variable and function parameter point same object in memory.

Python pass by value mean when you give value to function, function get copy of value, not original. When function change this copy, original value outside function no change. This happen with things like numbers and strings. So, changes inside function no affect original value.

FAQs about Python pass by reference and pass by value

Now, you maybe understand difference between pass by reference and pass by value in Python. But some common questions still come:

Q1: Is Python pass by reference or pass by value?

Python use pass-by object reference. This mean for things you cannot change (immutable), it act like pass by value. For things you can change (mutable), it act like pass by reference.

Q2: Can we change immutable type inside function in Python?

No, you cannot change things like number or string inside function. If you try, Python make new object and keep old one safe.

Q3: How Python work with list in function?

List can change, so if you give list to function, change inside function will also change original list outside.

Q4: If you change variable inside function, will original change?

Many think Python only pass by reference, but no. If you change variable inside function, original outside no change. Only local copy inside function change.

For example, look at this code where we try to change a list by reassigning it inside a function. You’ll see that the original list outside the function does not change because the reassignment only affects the local copy.

def reassign_list(in_list):
    in_list = ["new list"]  # This reassigns the local variable
    print("Inside fun() - list:", in_list)

nums = [1, 2, 3]
reassign_list(nums)
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

Quiz: Python Pass by Reference vs Pass by Value

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

Conclusion: Python Pass by Reference vs Pass by Value

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.

Share This Article
Leave a Comment

Leave a Reply

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