Python Pass By Reference Or Value

4 min read

Python uses pass by reference or value semantics that are often misunderstood because variables do not store values directly. Instead, names refer to objects in memory. Consider this: in Python, function arguments are passed through pass-by-assignment: a parameter receives a reference to the same object as the argument. Whether that reference appears to behave like pass-by-value or pass-by-reference depends on whether the object can be modified in place.

Introduction

Programming languages commonly describe argument passing as either pass by value or pass by reference. Python does not fit neatly into either traditional category.

The most accurate explanation is that Python uses pass-by-assignment, sometimes called call-by-sharing. When a function is called:

  1. The argument expression is evaluated.
  2. The resulting object is identified.
  3. The function parameter is assigned a reference to that object.

Both the caller and the function parameter therefore refer to the same object. On the flip side, the parameter itself is a new local name. Reassigning that name does not change the caller’s name, while mutating the shared object can affect the caller.

What Does “Pass by Value” Mean?

In a strict pass-by-value language, a function receives its own copy of the argument’s value. Modifying the parameter does not change the original variable Small thing, real impact. Took long enough..

Here's one way to look at it: in a language such as Java for primitive integers, a function might receive a numeric copy:

# Conceptual example from a pass-by-value language

def change_number(number):
    number = 100

Calling change_number(10) would not change the original value in many traditional pass-by-value languages.

Python behaves similarly when a parameter is reassigned:

def change_number(number):
    number = 100

value = 10
change_number(value)

print(value)  # 10

The function creates a new local name called number. Assigning 100 to that name does not reassign the original value.

This is why saying that Python is purely pass by value can be misleading. The parameter name is independently assigned, but the object it refers to may be shared.

What Does “Pass by Reference” Mean?

In pass by reference, a function receives direct access to the original variable or storage location. Changes made through the reference can alter the caller’s variable.

Python parameters are not references to variables. They are references to objects. That said, a function can modify a mutable object through that object reference:

def add_item(items):
    items.append("apple")

fruits = ["banana"]
add_item(fruits)

print(fruits)  # ["banana", "apple"]

The function did not replace the caller’s fruits variable. Instead, it modified the same list object that fruits referenced.

This behavior can make Python appear to use pass by reference.

Python Actually Uses Pass-by-Assignment

The most precise description is:

Python passes a reference to an object by value Most people skip this — try not to. Nothing fancy..

Another common term is pass-by-assignment, because the function parameter is assigned a reference to the object supplied by the caller Turns out it matters..

Consider this example:

def inspect_name(value):
    print(id(value))

number = 42
inspect_name(number)

The id() function returns an integer representing an object’s memory identity. The parameter value refers to the same object as number.

If value is reassigned inside the function, only the parameter receives the new object:

def reassign(value):
    value = 99

number = 42
reassign(number)

print(number)  # 42

If the object is mutated instead, both names observe the change:

def mutate(items):
    items.append(2)

items = [1]
mutate(items)

print(items)  # [1, 2]

The difference is not whether Python passes a reference or a value. The difference is whether the shared object is mutable.

Mutable Objects

A mutable object can be changed without creating a new object. Common mutable objects include:

  • Lists
  • Dictionaries
  • Sets
  • Custom objects created with classes
  • Some objects returned by libraries

For example:

def update_dictionary(data):
    data["score"] = 100

person = {"name": "Alex"}
update_dictionary(person)

print(person)
# {"name": "Alex", "score": 100}

The function modifies the original dictionary.

The same applies to lists:

def replace_list(items):
    items = ["new", "values"]

original = ["old", "values"]
replace_list(original)

print(original)  # ["old", "values"]

Here, replace_list() does not change the caller’s list. The assignment inside the function creates a new list and binds the local parameter to it.

Compare the two approaches:

def modify_list(items):
    items.append(3)

Just Went Up

Just Came Out

People Also Read

Also Worth Your Time

Thank you for reading about Python Pass By Reference Or Value. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home