From the course: Python Essential Training

Variables and scope - Python Tutorial

From the course: Python Essential Training

Variables and scope

- Welcome class. Today we're covering global and local variable scope interaction in Python. - Geez, that sounds tedious. Ignore them. I promise there's nothing crazy to memorize here. Just watch, learn, get a feel for it. It's easy. Previously, we saw how we could print out the arguments passed into function using asterisk args and asterisk asterisk kwargs. So you see it prints out that tuple and a dictionary there. But there's another way we can get access to all the variables in a Python function without using any asterisks at all, and that's the locals function. So let's rewrite our original function definition, perform operation num1, num2, operation equals by default sum. And we're going to print the output of this locals function that's built into Python, and we can call this perform operation one comma two, and let's call the operation multiply, okay? And what we get is a dictionary of all the variables that have been passed in, whether they're positional arguments or keyword arguments. And why is this function here called locals? Well, these are the variable names that are available locally inside the function. Remember, we can define variables to have any name we want in our function definition, and they're available anywhere inside that function. So obviously, I can't do something like this, num1. We get an error because I haven't defined num1 out here. So in Python we talk about local variables, things that are defined inside the function. We also talk about global variables, so things that are defined outside the function in the main body of the code. And conveniently enough, there's also a built-in Python function called globals that will get you all of these variables. Globals. Now you may not think that we have many global variables defined in here, but watch, if we run it, that's a lot of stuff. Some of these are built-in Python variables that will be useful later on when we start working with classes and packages. But there are also a lot of variables that Jupyter Notebooks uses to keep track of its data. See, all of this stuff in here is actually the contents of my Jupyter Notebook cells. There are lots of things going on behind the scenes in Python. So when you're talking about which variables you have access to in a particular line of code, that's called the scope. So we might talk about the global variable scope and the local variable scope, or the scope of variables inside this function. So let's look at how global and local variable scopes interact with each other by making a couple of functions and just sort of playing around with this. So let's make function one, and it's going to have the variables A and the variable B. It's just going to print out locals. And then we'll make function two, and that's going to have a varC and a varB again. Let's print out locals there too. Let's call function one with one comma two, function two with three comma four. So these two functions have their own local variable scope. They also have access to any variables inside the global scope. So if out here I define message equals some global data, and then let's print message, I may actually print that up there. Then print message. And you can see they have access to that. They don't, however, have access to each other's data. So if I try to print varA there, you can see varA isn't defined. It's defined in this function, not that one. But what if I define varA in the global scope? VarA equals two. What happens if I print out, let's say print varA here, and then also print varA here. Okay, so this is VarA being printed out for both functions. And you see that the first function prints out one because it's using varA that we passed in in the local scope. And the second function prints out two because it's using our definition here in the global scope. So when Python goes to look up the data associated with the variable name, it checks the local scope first. And then if that's not defined, it goes to the global scope. I can also say redefine message so that this first function gets its own local value for message. Some local data. And you see that prints out some local data and some global data. Another cool thing we can do is actually declare a function within another function, like this. Let me clean up some of this stuff. Inner function, varA, varB, print. Let's make this an f-string, inner function, local scope. It's going to be locals. And then out here, but still inside this function, we're going to call inner function with one, two, three, four, five, six. We'll call function again on the outside. Okay, so here we're calling function one, which defines this inner function and has its own local scope. And then it calls that inner function. Note that we can't call inner function outside of function one. So if I take this and move it down here, we get a syntax error because inner function isn't defined. Okay, another cool thing we can do is let's say we print these local variables, print locals down here, and then we call that again. And you see that when it prints out the local variables inside function one, we actually have inner function defined in there as a variable. Hmm. Could functions just be variables? Well, I think we'll find out shortly.

Contents