Loading video player…

Pass by Reference in Python: Best Practices (Overview)

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

For more information on concepts covered in this lesson, you can check out:

Download

Sample Code (.zip)

14.3 KB
Download

Course Slides (.pdf)

1.9 MB

00:00 Hello, and welcome to this course on best practices to replicate pass by reference in Python.

00:07 Python’s argument passing system is a bit different than those from other programming languages. In this course, you’ll be learning how that works and how to mimic certain functionality that other languages provide. This course is for you if you are already familiar with some basics of Python or maybe you’re coming to Python from another language, and you already know something about writing and calling functions. If not, Real Python has some excellent resources about functions you should check out.

00:41 In this course, you will learn a lot more about how Python’s argument passing mechanism works and how to replicate something called pass by reference, which is a useful feature of other programming languages.

00:57 Specifically, after this course, you should be able to explain what pass by reference is and why it’s useful, know how Python’s argument passing system is different from other languages’ argument passing mechanisms, show how arguments in Python work, use pass by reference on Python’s mutable types, and, most importantly, write Pythonic code to replicate the effects of pass by reference.

01:28 This course is based on a tutorial by Marius Mogyorosi. I do make some changes, so if you’re following along with that tutorial, you’ll notice some differences. One, I take a different approach to introducing pass by reference, so the first few lessons will not match the beginning of that tutorial.

01:47 My examples will be in C++ and not C#. There’s a lot less overhead to writing a C++ program, and hopefully that will make those examples a bit easier to understand.

02:00 These are the software tools I use in this course. If you’re used to other programs, then things will look a little bit different than what you’re used to. For my program files, I use Visual Studio Code.

02:12 I do have all of the code examples in the slide, but I’ll probably describe the examples in VS Code. All of my code files are available by a link in this course, so you’ll have both the slides and the actual files to refer to when wanting to look at the examples.

02:28 I’m working on a Mac, and I use iTerm for my terminal shell. For other systems, you might be looking at other terminal programs or maybe something called a command shell or a PowerShell. For interactive Python, I use a program called ptpython as my REPL.

02:45 Its syntax coloring is very effective for these video lessons.

02:51 So, let’s get started! In the next lesson, you’ll look at passing argument values to parameter variables.

Become a Member to join the conversation.