Skip to content

Python Pass by Reference: Understanding Object Assignment and Function Arguments

[

Pass by Reference in Python: Background and Best Practices

by Marius Mogyorosi

After gaining some familiarity with Python, you may notice cases in which your functions don’t modify arguments in place as you might expect, especially if you’re familiar with other programming languages. Some languages handle function arguments as references to existing variables, which is known as pass by reference. Other languages handle them as independent values, an approach known as pass by value.

If you’re an intermediate Python programmer who wishes to understand Python’s peculiar way of handling function arguments, then this tutorial is for you. You’ll implement real use cases of pass-by-reference constructs in Python and learn several best practices to avoid pitfalls with your function arguments.

In this tutorial, you’ll learn:

  • What it means to pass by reference and why you’d want to do so
  • How passing by reference differs from both passing by value and Python’s unique approach
  • How function arguments behave in Python
  • How you can use certain mutable types to pass by reference in Python
  • What the best practices are for replicating pass by reference in Python

Defining Pass by Reference

Before you dive into the technical details of passing by reference, it’s helpful to take a closer look at the term itself by breaking it down into components:

  • 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 a variable that already exists in memory rather than an independent copy of that variable.

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.

Contrasting Pass by Reference and Pass by Value

Many programming languages, such as C#, handle function arguments as references to variables, which is known as pass by reference. This means that changes made to the argument within the function will modify the original variable.

In Python, however, the situation is a bit different. Python uses a combination of pass by value and pass by reference, but it can behave differently depending on the type of the argument.

Using Pass by Reference Constructs

One of the benefits of pass by reference is the ability to avoid creating duplicate objects. When you pass an object by reference to a function, any changes made to that object within the function will be reflected outside of it as well.

Another useful aspect of pass by reference is the ability to return multiple values from functions. In Python, you can use pass by reference to achieve this by modifying mutable objects and returning them as part of a tuple.

Additionally, pass by reference can be used to create conditional multiple-return functions. By passing mutable objects to a function and modifying them based on certain conditions, you can effectively return different values depending on the state of the object.

Passing Arguments in Python

Understanding assignment in Python is important for grasping how function arguments work. In Python, variables are essentially references to objects. When you assign a value to a variable, you’re actually binding that variable to the object in memory.

Function arguments in Python behave similarly to variable assignment. When you pass an argument to a function, you’re essentially creating a new reference to the same object.

Exploring function arguments in Python involves understanding the difference between mutable and immutable types, as well as the relationship between the function parameter and the argument passed to it.

Replicating Pass by Reference With Python

While Python predominantly uses pass by value, there are ways to replicate pass by reference behavior. By following best practices, you can modify mutable objects and have those changes persist outside the scope of the function.

One best practice for achieving pass by reference behavior is to return and reassign. Instead of modifying arguments directly within a function, you can return a modified version of the object and assign it back to the variable.

Another best practice is to use object attributes. By modifying attributes of an object, you can effectively achieve pass by reference behavior.

Using dictionaries and lists is yet another best practice for replicating pass by reference. Because dictionaries and lists are mutable objects, any modifications made to them within a function will be reflected outside of it.

Conclusion

In this tutorial, you learned about pass by reference in Python and how it differs from both pass by value and other languages’ implementation of pass by reference. You explored various use cases for pass by reference, such as avoiding duplicate objects, returning multiple values, and creating conditional multiple-return functions. Additionally, you discovered best practices for replicating pass by reference behavior in Python using return and reassign, object attributes, and dictionaries and lists. By understanding these concepts and following best practices, you can effectively work with function arguments in Python.