Pass by Reference in Python: Best Practices

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 course 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 course, 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

What’s Included:

Downloadable Resources:

About Howard Francis

Howard is an avid Pythonista and records video courses for Real Python. He has been teaching beginning programming at the college level for over 20 years in C++, Java, C#, and most recently Python.

» More about Howard

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

Participant Comments

mckown on Nov. 9, 2021

Just when I thought Halloween was over. Wait! What?

How many of us were swayed to try Python because of its simple syntax, easy-to-use REPL, and seductive lists vs limited arrays? Like high schoolers wandering into that empty, dark house.

I should have hesitated when I learned that there are no private variables. Nothing is private in Python. I should have run when I found that variables can be assigned any type willy-nilly. Strong data types are non-existent in Python. But I stayed. And, now, despite all the years of learning pass by reference and pass by value–common concepts that every American child learns by heart, the demons arise to sweep all that away to pass by assignment. It makes one’s hackles rise.

There are too many plots and subplots in this course. Can you make it through without having your programmer’s blood sucked out with every “AHA” moment. This course is good. SCARY good.

I’m looking forward to Howard Francis’ (and team!) next spooktacular installment.... If I ever recover my soul.

« Browse All Courses