If you’d like to take more video courses on scopes, then check out:
Functions: Variables, References, and Scope (Part 2)
Variables, References, and Scope:
globals(). Scope is something which is initially difficult to understand when starting programming, and
vars() allow you to understand scope a little better.
This is best demonstrated in the context of a program, so here you can see a program with
a being equal to
b being equal to the string
c being equal to the string
As you may know, within a function, the same variable names can be redefined with different values, and they won’t refer to the same actual variable. So here, defining a function where
a is being set to
b is being set to the string
'local', and then those two values will be printed out.
Meanwhile, in our main scope, we will print out the values of
then call the
which will print those values out, having defined local values of them. And then we’re going to print
b again to see whether or not they’ve been permanently altered while in
01:23 Let’s see that program running.
You can see that here we’re in the global scope, so that’s this line here, line 10. And there we have the local versions of
b being printed out.
And then once we’re back in the global scope,
b are back with the values we would expect there. Now, sometimes it’s difficult to know what variables have been defined or are available to you in any given scope. This example is a fairly simple program, but understanding what’s going on in a more complicated program can be more difficult. One function which is particularly useful is
If we print out
vars(), it will show what variables are available in this scope, and it will print them out in the form of a dictionary.
Running the program, you can see that where we’ve printed out
vars(), the available variables in this local scope are
a is equal to
b is equal to
'local', which is what was printed out. However, if we take that code from line 9—so, exactly the same thing,
print(vars())—if we put that at line 14, after we’re back in the global scope you’ll see that there are more values available.
So here, we have this much bigger dictionary of values, but the most important things for us at this point is that
a is equal to
b is equal
c has a value of
So different values are available depending on what scope we’re in. As you’ve seen, these are the vars which were available in the global scope, and instead of printing
vars(), you can print using the function
globals(), which gives us the global scope variables at any point.
03:26 So here, you can see it gives the same output because the vars are the global vars when printed out in the global scope. However, I think this might muddy the water slightly, because it’s a different function.
It doesn’t illustrate the difference in that variables available in the function and the global scope are the same, because you could print
globals() out while inside the function… So if that gets printed out while inside the function—so if we move
globals() to be printed inside the function—you can see that these are the local variables and these are the global variables. So if this is printed out, you can see at exactly the same time the global value of
So that can get a little bit confusing, but it’s certainly really useful to be able to print out
vars() because you can understand what variables are available to you at that point in that scope, and if you’re unsure what scope a function is running in, you can use
vars() to understand what’s going on.
But there is something else which also muddies the water slightly, which is if we print out
c, it’s not defined within this function, so if we can’t find it in the local scope, it will then see the value of it from the global scope. So if we run that, you can see that despite the fact that it’s not defined in the local scope,
c is accessible from the global scope from line 3, because it’s not defined within that function.
05:08 This is something you need to spend a bit of time getting your head around but once you understand it, it will help with your understanding of what’s going on generally.
But it’s easy to tie yourself up in knots, particularly if you use the same variable names inside a function as outside a function. So the local
a is different from the global
a, which is why it can get confusing and it’s a good idea not to name your local variables with the same name as a global variable.
05:36 Many IDEs such as PyCharm will warn you about this, and that you’re redefining a global variable with a local value. By avoiding this local reuse of global variable names, you’ll remove one step that you need to make in understanding what’s going on in your program.
Really like that
vars() function. I can see that being very handy for debugging. You know, as opposed to printing out variables one-by-one.
For my own learning - this lecture on Variables, References and Scope (part 2) - it would have been easier to visualise the code and output if the recording showed a split screen terminal/vscode next to each other. Simply seeing the output in terminal was very messy as it all blends into one sprawling output. At least it does when I view it. However if we could have code on one side and terminal output when run alongside - I think that would aid learning?
Become a Member to join the conversation.
pshekhar2707 on March 4, 2020
Its a good basic course to start with.