Does Python Have Pass-by-Value VS Pass-by-Reference Variables? (38/100 Days of Python)

Martin Mirakyan
4 min readFeb 8, 2023
Day 38 of the “100 Days of Python” blog post series covering passing variables by reference versus passing them by value

When writing code, it’s important to understand the difference between pass-by-value and pass-by-reference. In this tutorial, we’ll explore both concepts and their implications when passing arguments to functions in Python.

Pass by Value VS Pass by Reference visualization. Reference: link

Passing Variables by Value in Python

Pass by value refers to the way that a function’s arguments are passed to the function. In this method, the value of the argument is copied and passed to the function. If the argument is a primitive data type (such as a string or an integer), this means that the function cannot modify the original value. Instead, the function operates on a copy of the value.

This makes sure that the original data is not modified:

def increment(x):
x += 1
return x

a = 5
b = increment(a)
print(a) # 5 (the value of `a` is not changed)
print(b) # 6 (b is assiged the new value returned by the function)

In this example, we see that a is passed to the increment function as an argument. The function increments the value of x by 1 and returns the result. When we print a and b after the function call, we can see that a remains unchanged, while b contains the incremented value.

Passing Variables by Reference in Python

Passing by reference, on the other hand, refers to the way that a function’s arguments are passed to the function. In this method, the function receives a reference to the original argument. This means that if the argument is a mutable data type (such as a list or a dictionary), the function can modify the original data:

def append_to_list(lst):
lst.append(1)
return lst

a = [5]
b = append_to_list(a)
print(a) # [5, 1] (the initial list was modified)
print(b) # [5, 1] (so now both a and b have the same value)

In this example, we see that the list a is passed to the append_to_list function as an argument. The function modifies the list by appending the value 1 to it. When we print a and b after the function call, we can see that both contain the modified list with the value 1 appended to it.

Why Does This Happen?

In Python, variables can store values of different data types. These data types can be divided into two categories: mutable and immutable.

In both cases we had the same syntax for passing the variables to the function, yet, the final result was completely different. That happened because, in the initial example, the value passed to a function was an integer, which is an immutable data type, while the second example involved passing a list, which is a mutable data type.

Mutable Data Types

Mutable data types are data types that can be changed or modified after they have been created. The most common mutable data types in Python are lists, dictionaries, and sets:

# List
a = [1, 2, 3]
b = a
a[1] = 4
print(a) # [1, 4, 3]
print(b) # [1, 4, 3]

# Dict
a = {'key1': 'value1', 'key2': 'value2'}
b = a
a['key2'] = 'new_value2'
print(a) # {'key1': 'value1', 'key2': 'new_value2'}
print(b)

In this example, we see that we can modify the values of lists and dictionaries after they have been created.

Immutable Data Types

Immutable data types are data types that cannot be changed or modified after they have been created. The most common immutable data types in Python are integers, floats, strings, and tuples:

# Integer
a = 5
b = a
a += 1
print(a) # 6
print(b) # 5

a = 'hello'
b = a
a += ' world'
print(a) # 'hello world'
print(b) # 'hello'

In this example, we see that we cannot modify the values of integers and strings after they have been created. Instead, we have to create a new value by performing operations on the original value.

So, in reality, Python doesn’t have a real notion of pass-by-value VS pass-by-reference variables (as other languages do — C++ is a great example). In Python, it comes down to the data types themselves. We can think of immutable variables as pass-by-value, while mutable ones as pass-by-reference.

What’s next?

--

--

Martin Mirakyan

Software Engineer | Machine Learning | Founder of Profound Academy (https://profound.academy)