Pass by Reference in Python: Best Practices (Summary)
Python works differently from languages that support passing arguments by reference or by value. Function arguments become local variables assigned to each value that was passed to the function. But this doesn’t prevent you from achieving the same results you’d expect when passing arguments by reference in other languages.
In this course, you learned:
- How Python handles assigning values to variables
- How function arguments are passed by assignment in Python
- Why returning values is a best practice for replicating pass by reference
- How to use attributes, dictionaries, and lists as alternative best practices
For more information on concepts covered in this lesson, you can check out:
- The Walrus Operator: Python 3.8 Assignment Expressions | Real Python Tutorial
- Immutability in Python | Real Python Video Course
- Namespaces and Scope in Python | Real Python Tutorial
- Python Scope & the LEGB Rule: Resolving Names in Your Code | Real Python Tutorial
- Python Constants: Improve Your Code’s Maintainability
- How to Use Type Hints for Multiple Return Types in Python
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 Good work! Let’s go over everything you’ve done and learned. You’ve learned how Python binds variable names to objects in assignment statements. You’ve learned how Python’s pass by assignment mechanism works.
00:14 You’ve learned techniques in Python, like return and reassign, to mimic pass by reference. And you’ve learned how pass by assignment is similar to pass by reference when using mutable objects as arguments.
00:29 Now you can compare and contrast pass by reference, pass by value, and pass by assignment; describe how argument passing works in Python; and write Pythonic code to replicate the features of pass by reference.
00:45 Along the way, you also learned some things about mutable objects, Python 3.8’s assignment expressions, and some information about namespaces and scope, and hopefully quite a lot more as well. Remember, Real Python has some excellent resources on these topics, some of which are linked below.
01:05 I hope you enjoy this course and I’ll see you next time here at Real Python.
Become a Member to join the conversation.
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.