Python (Fall 2026) Module 7-7: Variable Scope and Globals
Watch on YouTube →
Overview
Bill Moseley explains Python variable scope through an `x` example: an `x` assigned inside a function is local and does not change an `x` outside it, while a function can read a global variable when no local `x` is defined. Declaring `global x` lets the function reassign the outer variable, but ties the function to its surrounding program; Moseley recommends parameters and return values for reusable black-box functions.
Key takeaways
- An assignment to `x` inside a Python function creates a local variable by default, so changing that local `x` does not change an outer `x`.
- A function can read a global variable such as `x = 20` if it does not create a local variable with the same name.
- The declaration `global x` makes assignments to `x` inside the function affect the program-level variable.
- Using `global` couples a function to a variable in its surrounding program, which can make the function harder to reuse elsewhere.
- Parameters and return values are Moseley's preferred way to pass information into and out of reusable functions.
Chapters
- Reusable Python functions communicate through parameters and return values, allowing callers to use them without knowing their internal implementation.
- In the `showVars` example, assigning `x = 33` inside the function creates a function-scoped variable.
- When the program sets `x = 20` outside the function, that outer value remains 20 after `showVars` runs.
- A variable assigned outside a function has global scope and can be read inside the function when no local variable with that name is assigned.
- Defining a local `x` inside `showVars` shadows the global `x` but does not change its value.
- Moseley recommends avoiding identical variable names inside and outside functions to reduce confusion about which `x` is being used.
- Adding `global x` tells Python that assignments to `x` inside the function should reassign the global variable.
- In the example, setting `x = 33` after declaring `global x` changes the outer `x` from 20 to 33.
- A function that relies on a global `x` is less portable because another program must provide that global variable.
- Moseley recommends returning 33 to the caller and assigning the result where needed, preserving the function's black-box design.
Transcript excerpt
0:01 Now, reviewing what we talked about last time, remember that when we build a function, if we, if we're building it well, we should be able to use that function in a variety of different programs. And so, the way we communicate using a function is this whole black box idea, which means we don't worry about how it works. All we need to know is kind of what it does, as well as what goes into it and what comes out of it. If we understand those things about a function, we can use and reuse a function. We can share functions with other programmers. And honestly, that's one of the great things about Python, is that people have written all kinds of functions and libraries and stuff out there that they share to allow us to do all kinds of things with Python without having to know all the nitty-gritty of how that works. So anytime we're doing this, we want to build our
1:00 functions so that they operate with parameters and return values as sort of a black box. And so that brings me to variable scope. Variable scope refers to where information is available. Now in this very simple function, showVars, or show variables, all this does is it says, inside the function I'm creating a variable called x, I'm setting it to the value of 33, and then I'm printing, right now x equals, and it prints the value of x. So all it's going to do every time I run that function is going to say x = 33. But if I write this code and I start out with x = 20, okay, outside the function, in my main line of code, I'm saying x = 20. And then I'm printing it, x = 20.
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.