Loading video player…

Introducing and Refactoring the Code

Have you been wondering how scopes and closures work in Python? Maybe you’ve just heard about object.__closure__, and you’d like to figure out what exactly it does. In this Code Conversation video course, you’ll use the debugger Thonny to walk through some sample code and get a better understanding of scopes and closures in Python.

In this Code Conversation video course, you’ll:

  • Clarify code by refactoring it with descriptive names
  • Learn how functions access variables in local and nonlocal scopes
  • Understand how inner and outer function calls open and close their own scopes

You’ll also take a deep dive into the inner workings of Python by inspecting dunder objects to find out how Python handles and stores variables.

To get the most out of this Code Conversation, you should be familiar with scopes and variables in Python. You should also be comfortable defining your own functions and distinguishing between inner and outer functions.

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

Download

Sample Code (.zip)

579 bytes
Download

Course Slides (.pdf)

616.0 KB

00:00 Welcome to a Real Python Code Conversation. In this guided walkthrough, you will learn about scopes and closures in Python using a practical example and stepping through it using a debugger.

00:14 Maybe you’ve been wondering how do closures and scopes work in Python, and like one of our participants during the office hours, you just heard about object.__closure__, and you’re just wondering what is going on here.

00:28 And there is a nice answer on Stack Overflow by Martijn Pieters that goes into depth about what is included in object.__closure__, but it’s kind of still hard to read, and there’s a piece of code in there that is maybe not that easy to understand if you don’t have the context, and if you don’t have a lot of experience with scopes and closures.

00:49 So what I’m going to do today in this video is I’m going to take this code, pick it apart, and walk you through it step by step. And in the process of doing this, you’re going to learn about scopes and closures in Python, and hopefully it’s going to help you to get a better sense of what’s actually going on in here. Also keep in mind that we have articles on this topic.

01:09 We have one called Namespaces and Scope in Python and another one about Python Scope & the LEGB Rule: Resolving Names in Your Code. So check those out as well if you still have questions, or if you’re curious to read more in-depth about this topic. So to get started, I’m going to just take this piece of code and copy it, and then head over to my editor.

01:31 And for this walkthrough, I’m going to use Thonny. This is a beginner editor that gives you a really nice way of stepping through a debugging session, and this is why I’m going to use it for this video.

01:43 So I’m just pasting this code in here, but to be honest, it’s a bit hard to understand what’s going on here if you have such non-descriptive names. So I will change those a little just to make it easier to keep track of what’s going on later, when I’ll walk you through this code. So foo(), this is going to be an outer function, so I’ll just call it outer() and we’ll have to change this in the function call as well.

02:07 Then you have bar() here, which is going to be the inner function,

02:12 so I’ll call it inner() in all the occurrences.

02:20 And then spam. This is really just a message that you want to print out, So I’m going to rename that to message,

02:32 and let’s keep a classic hello world as the message that you’re printing. And finally, you’re returning inner function. One more thing out here.

02:42 What you’re really returning is the inner function objects, so I’m going to call it inner_returned.

02:52 And that about finishes up this quick refactoring that is really just renaming the different names of the functions and of the variables that I used in this piece of code.

03:01 And it’s going to make it easier for us to keep track of what’s actually happening in this code when we step through it. And that’s about it. So this is going to be a quick course that just goes about understanding the scopes and closures in Python.

03:14 And in the next lesson, you’ll get started stepping through this code example, using the debugger in Thonny.

Become a Member to join the conversation.