What Is Pass By Value And Pass By Reference In Python

Python’s parameter passing mechanism is often described as “pass by object reference” or “pass by assignment,” which can be perplexing due to its departure from the traditional concepts of “pass by value” and “pass by reference” in other programming languages.

In the Python context, when a variable is passed as an argument to a function, it involves passing a reference to the object that the variable references, rather than duplicating the object itself. Consequently, modifications made to the object within the function will impact the original object beyond the function’s scope. Nevertheless, the potential for confusion arises from the fact that the behavior can resemble both “pass by value” and “pass by reference,” contingent on the particular scenario.

To illustrate this:

  1. Immutable objects (like numbers, strings, tuples): For immutable objects, it behaves somewhat like “pass by value” because you can’t modify the original object inside the function. Any changes made will result in a new object being created.
  2. Mutable objects (like lists, dictionaries): For mutable objects, it behaves more like “pass by reference” because you can modify the object itself inside the function, and those changes will be reflected outside the function as well.

Let’s take some examples:

def modify_immutable(x):
    x = x + 1
    print("Inside function:", x)

num = 5
modify_immutable(num)
print("Outside function:", num)
Code language: Python (python)

Output:

Inside function: 6
Outside function: 5
Code language: Python (python)

In this case, the integer num is immutable, so a new object is created inside the function when modifying it. The original num remains unchanged outside the function.

def modify_mutable(lst):
    lst.append(4)
    print("Inside function:", lst)

my_list = [1, 2, 3]
modify_mutable(my_list)
print("Outside function:", my_list)
Code language: Python (python)

Output:

Inside function: [1, 2, 3, 4]
Outside function: [1, 2, 3, 4]Code language: Python (python)

Here, the list my_list is mutable, so modifications made to it inside the function are reflected in the original list outside the function.

In summary, Python’s parameter passing mechanism combines aspects of both “pass by value” and “pass by reference” but is more accurately described as “pass by object reference.” The key takeaway is to understand how mutability affects the behavior when working with function arguments.

Read More;

    by
  • Muhammad Nabil

    I am a skilled and experienced Python developer with a huge passion for programming and a keen eye for details. I earned a Bachelor's degree in Computer Engineering in 2019 from the Modern Academy for Engineering and Technology. I am passionate about helping programmers write better Python code, and I am confident that I can make a significant contribution to any team. I am also a creative thinker who can come up with new and innovative ways to improve the efficiency and readability of code. My specialization includes Python, Django, SQL, Apache NiFi, Apache Hadoop, AWS, and Linux (CentOS and Ubuntu). Besides my passion for Python, I am a solo traveler who loves Pink Floyd, online video games, and Italian pizza.

Leave a Comment