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.
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
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 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.
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.
Become a Member to join the conversation.