Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Functions: Variables, References, and Scope (Part 2)

Give Feedback

00:00 Variables, References, and Scope: vars() and globals(). Scope is something which is initially difficult to understand when starting programming, and globals(), locals(), and vars() allow you to understand scope a little better.

00:16 This is best demonstrated in the context of a program, so here you can see a program with a being equal to 1, b being equal to the string 'global', and c being equal to the string 'another global'.

00:29 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 2, b is being set to the string 'local', and then those two values will be printed out.

00:56 Meanwhile, in our main scope, we will print out the values of a and b,

01:04 then call the function(),

01:07 which will print those values out, having defined local values of them. And then we’re going to print a and b again to see whether or not they’ve been permanently altered while in function().

01:23 Let’s see that program running.

01:32 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 a and b being printed out.

01:43 And then once we’re back in the global scope, a and 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 vars().

02:08 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.

02:19 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 2, and 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.

02:49 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 1, b is equal 'global', and c has a value of 'another global'.

03:04 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.

03:42 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 a is 1 and b is 'global', whereas a is 2 and b is 'local'.

04:19 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.

04:39 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.

05:16 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.

pshekhar2707 on March 4, 2020

Its a good basic course to start with.

Alain Rouleau on Aug. 7, 2020

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.

Become a Member to join the conversation.