Is python pass by value or pass by reference?

Developers jumping into Python programming from other languages like C++ and Java are often confused by the process of passing arguments in Python. The object-centric data model and its treatment of assignment is the cause behind the confusion at the fundamental level. 

In the article, we will be discussing the concept of how to pass a value by reference in Python and try to understand pass-by-reference examples in Python.

Pass by value and pass by reference in Python

You might want to punch something after reading ahead, so brace yourself. Python’s argument passing model is neither “Pass by Value” nor “Pass by Reference” but it is “Pass by Object Reference”. 

The paradigms of “Pass by value”, “Pass by Reference” and “Pass by object Reference” can be understood by exploring the below example functions. Look into the two functions defined below:

Python3

def set_list(list):

    list = ["A", "B", "C"]

    return list

def add(list):

    list.append("D")

    return list

my_list = ["E"]

print(set_list(my_list))

print(add(my_list))

Output:

['A', 'B', 'C']
['E', 'D']

Now, let’s explore the above code, 

The variable is not the object

Here “a” is a variable that points to a list containing the element “X” and “Y”. But “a” itself is not the list. Consider “a” to be a bucket that contains the object “X” and “Y”. 

 a = ["X", "Y"]

Is python pass by value or pass by reference?

What is Pass by Reference In Python?

Pass by reference means that you have to pass the function(reference) to a variable which refers that the variable already exists in memory. 

Here, the variable( the bucket) is passed into the function directly. The variable acts as a Package that comes with its contents(the objects).

Is python pass by value or pass by reference?

In the above code image both “list” and “my_list” are the same container variable and therefore refer to the exact same object in the memory. Any operation performed by the function on the variable or the object will be directly reflected by the function caller. For instance, the function could completely change the variable’s content, and point it at a completely different object: 

Is python pass by value or pass by reference?

Also, the function can reassign the contents of the variable with the same effect as below: 

Is python pass by value or pass by reference?

To summarize, in pass-by-reference the function and the caller use the same variable and object.

What is Pass by Value In Python?

In this approach, we pass a copy of actual variables in function as a parameter. Hence any modification on parameters inside the function will not reflect in the actual variable.

Is python pass by value or pass by reference?

The same is true for any operation performed by the function on the variable or the object 

Is python pass by value or pass by reference?

To summarize the copies of the variables and the objects in the context of the caller of the function are completely isolated.

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.
Example 1: 
 

Python3

string = "Geeks"

def test(string):

    string = "GeeksforGeeks"

    print("Inside Function:", string)

test(string)

print("Outside Function:", string)

Output 
 

Inside Function: GeeksforGeeks
Outside Function: Geeks

Example 2 
 

Python3

def add_more(list):

    list.append(50)

    print("Inside Function", list)

mylist = [10,20,30,40]

add_more(mylist)

print("Outside Function:", mylist)

Output 
 

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.
 

Python3

a = "first"

b = "first"

print(id(a))

print(id(b))

print(a is b)

Output 
 

110001234557894
110001234557894
True

Now, let’s try and understand this better with another example.
Example 2:
 

Python3

a = [10, 20, 30]

b = [10, 20, 30]

print(id(a))

print(id(b))

print(a is b)

Output 
 

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.
Example 3: 
 

Python3

def foo(a):

    a = "new value"

    print("Inside Function:", a)

string = "old value"

foo(string)

print("Outside Function:", string)

Output:
 

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 foo, 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 foo , 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.
 

Python3

def foo(a):

    a[0] = "Nothing"

bar = ['Hi', 'how', 'are', 'you', 'doing']

foo(bar)

print(bar)

Output: 
 

['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. 
 


Is pass

Python uses pass-by-value. It is also true that all variables in Python are references to values, but that doesn't change the fact that the parameter passing is by value. The page itself acknowledges that Java passes by value, and non-primitive types in Java are stored as references.

Are Python dictionaries passed by reference?

In essence, one can say that mutable objects like dictionaries, sets, and lists are passed by reference. Immutable objects like int , str , tuple are passed by value.

What is pass by reference and pass

Pass by reference means that you have to pass the function(reference) to a variable which refers that the variable already exists in memory. Here, the variable( the bucket) is passed into the function directly. The variable acts as a Package that comes with its contents(the objects).

Does Python pass int by reference?

Object Oriented Programming Fundamentals If you pass immutable arguments like integers, strings or tuples to a function, the passing acts like Call-by-value. It's different, if we pass mutable arguments. All parameters (arguments) in the Python language are passed by reference.