Skip to content

Pass Reference in Python

[

Pass by Reference in Python: Background and Best Practices

by Marius Mogyorosi

Defining Pass by Reference

Before diving into the technical details of passing by reference in Python, it’s important to understand what it means.

  • Pass means to provide an argument to a function.
  • By reference means that the argument you’re passing to the function is a reference to an existing variable in memory, rather than an independent copy of that variable.

When you pass an argument by reference, any operations performed on this reference will directly affect the variable to which it refers. In other words, changes made to the argument inside the function will be visible outside the function.

Contrasting Pass by Reference and Pass by Value

Pass by reference in Python is different from both pass by value and from how some other programming languages handle function arguments.

In pass by value, a copy of the value is made and passed to the function. Therefore, any changes made to the argument inside the function would not affect the original variable.

In contrast, pass by reference passes the actual reference to the variable, so changes made to the argument inside the function will affect the original variable.

Python, however, does not strictly follow either of these approaches. It uses a unique approach where arguments are passed by reference, but immutable objects (such as strings and numbers) are passed by value. This means that changes made to an immutable object within a function will not affect the original object.

Using Pass by Reference Constructs

Now that you understand the concept of passing by reference, let’s explore some use cases and best practices.

Avoiding Duplicate Objects

One advantage of passing by reference is that it allows you to avoid creating duplicate objects. Instead of creating a new copy of an object every time it is passed to a function, you can modify the existing object directly.

Returning Multiple Values

Passing by reference also allows you to return multiple values from a function. By modifying the referenced objects, you can effectively return multiple results without using a container object like a tuple.

Creating Conditional Multiple-Return Functions

Pass by reference can be used to create conditional multiple-return functions. By modifying the referenced objects based on certain conditions, you can choose which values to return.

Passing Arguments in Python

To understand how arguments are passed in Python, it is important to understand how assignment and function arguments work.

Understanding Assignment in Python

When you assign a value to a variable in Python, you are actually binding the variable name to an object. The variable becomes a reference to the object.

Exploring Function Arguments

In Python, function arguments are also references to objects. This means that when you pass an argument to a function, you are actually passing a reference to the object.

Replicating Pass by Reference With Python

Although Python does not strictly follow pass by reference, you can replicate pass by reference behavior using certain mutable types.

Best Practice: Return and Reassign

One way to replicate pass by reference in Python is to return the modified object from a function and reassign it to the original variable.

Best Practice: Use Object Attributes

Using object attributes allows you to modify the referenced object directly, replicating pass by reference behavior.

Best Practice: Use Dictionaries and Lists

Mutable objects like dictionaries and lists can be modified directly, allowing you to replicate pass by reference behavior.

Conclusion

Pass by reference in Python is a unique concept that differs from both pass by value and how other programming languages handle function arguments. By understanding how it works and using best practices, you can effectively work with function arguments in Python and avoid common pitfalls.