DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Originally published at flexiple.com

Python - pass by reference

In this tutorial, we will learn how to use pass by reference in Python. Variables work differently in Python than in any other programming language known to us. It is a memory location with a unique address whereas in Python, variables are not declared beforehand but the data decides the data type.

Table of Contents

  1. What is pass by reference
  2. Pass by reference vs value in Python
  3. Python Object Model
  4. Closing Thoughts

What is pass by reference?

Before we jump into the technical details, let's first see what does pass and by reference actually mean.

By pass we mean to provide an argument to a function. Whereas by reference means that the argument that has been passed to the function is a reference to a variable that is already existing in the memory. Now that we have cleared that, now we can learn about pass by reference.

In pass by reference, the variable is passed into the function directly while the variable acts as a Package that comes with the objects. Because you've given the function a reference to an existing variable, any operations you execute on it will have a direct effect on the variable it refers to.

Pass by reference vs value in Python

When you give function parameters via reference, you're just passing references to values that already exist. When you pass arguments by value, on the other hand, the arguments become independent copies of the original values.

Pass by value

Any other operation performed will not have any effect on the original value as the argument passed to the function is copied. It only changes the value of the copy created within the function.

Input:

def function(int):
  int+=100
  print("Inside function call ",int)

int=100
print("Before function call ",int)
function(int)
print("After function call ",int)
Enter fullscreen mode Exit fullscreen mode

Here, a copy of the argument is created, and changes are made to that copy such that the original value is not affected. As a result, the original value is printed after the function call.

Output:

Before function call 100                                       
Inside function call 200
After function call 100 
Enter fullscreen mode Exit fullscreen mode

In this, we assigned the variable the value '100' and changed it to '200', but the change was not seen outside the function i.e. int is still '100'. Hence proved, it is pass by value method.

Pass by reference

This method passes a reference to the original arguments, and any operation performed on the parameter affects the actual value. It alters the value in both function and globally.

Input:

def function(int):
    int.append('B')print("Inside function call",int)

int=['A']
print("Before function call",int)
function(int)
print("After function call",int)
Enter fullscreen mode Exit fullscreen mode

When a list is used, its reference is supplied into the function, and any modifications to the list have an effect even after the method has been called.

Output:

Before function call ['A']          
Inside function call ['A', 'B']                  
After function call ['A', 'B']
Enter fullscreen mode Exit fullscreen mode

In the above example, function() returns a string and also modifies the value of int. This shows that Python supports the pass by reference method.

Python Object Model

It's best if we first understand what Python objects are and how to characterize them. In Python, everything is an object. Object's identity, data type and object's content characterize every object.

# the identity of `int`
id(int)
3578332784044       

# the type of `int`
type(int)

# the contents of `int`
int [1, 2, 3]           
Enter fullscreen mode Exit fullscreen mode

As we can see above, id is the built-in function you use to query the identity of an object, and type is the built-in function you use to query the type of an object.

Closing Thoughts

Python operates differently than the other programming languages when it comes to the moving of the arguments. Since you’re giving the function a reference to an existing variable, all operations performed on this reference will directly affect the variable to which it refers. One can learn about other Python-related concepts here.

Top comments (0)