Python - pass by reference
Tags: python, tutorial, programming, beginners
Published: 2022-03-23 07:01:33

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](#what-is-pass-by-reference)
2.  [Pass by reference vs value in Python](#pass-by-reference-vs-value-in-python)
3.  [Python Object Model](#python-object-model)
4.  [Closing Thoughts](#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<span id="section2"></span> 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](https://www.w3schools.com/python/gloss_python_function_arguments.asp) 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)

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 

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)

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']

<span id="section3"></span>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]           

As we can see above, id is the built-in function you use to query <span id="section4"></span>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](https://flexiple.com/python-return-multiple-values) here.