In Post #95, we did a deep dive into the “local scope”—the private, temporary workspace inside a function. We learned that local variables are created when a function is called and destroyed when it finishes. Now, let’s zoom out from the “inside of the box” to look at the big picture.
In this post, we’ll formally explore the global scope. We’ll define what it is and see how functions can interact with the variables that live in it.
Defining the Global Scope
The global scope is the main, top-level environment of your Python script. Any variable that you define outside of a function (at the highest level of indentation) is a global variable.
Unlike local variables which are temporary, global variables exist for the entire lifetime of your program. They are created when your script starts running and are only destroyed when the script finishes.
# This variable is in the global scope
player_name = "Gandalf"
def a_function():
# This variable is in the local scope of a_function
local_var = "Hello"
# This print statement is also in the global scope
print(f"This is a global variable: {player_name}")
Reading Global Variables from a Local Scope
One of the key rules of scope is that code inside a local scope (a function) can read the value of variables in the global scope.
When a function needs to use a variable, it first looks for it in its own local scope. If it can’t find it there, it looks “outward” into the global scope to see if it exists there.
# This is a global variable
character_class = "Wizard"
def print_character_sheet():
# This function can READ the global 'character_class' variable
print(f"Character Class: {character_class}")
# When we call the function, it successfully finds and prints the global variable
print_character_sheet()
The output will be: Character Class: Wizard
.
The “Shadowing” Pitfall: An Accidental Local Variable
This is where things get interesting and often trip up beginners. What happens if you try to assign a new value to a variable inside a function that has the same name as a global variable?
You might expect this to change the global variable, but it doesn’t. Instead, Python creates a brand new local variable with the same name. This local variable “hides” or shadows the global variable for the duration of the function call.
player_level = 10 # Global variable
def level_up():
# This creates a NEW LOCAL variable also named 'player_level'
# It "shadows" the global one, but does not change it.
player_level = 11
print(f"Inside the function, the player is level: {player_level}")
print(f"Before the call, the global player level is: {player_level}")
level_up()
print(f"After the call, the global player level is still: {player_level}")
The output clearly shows that the global variable was not affected:
Before the call, the global player level is: 10
Inside the function, the player is level: 11
After the call, the global player level is still: 10
A Word of Caution on Global Variables
While it’s convenient to be able to read global variables, relying on them too heavily is generally considered bad practice for larger programs. The preferred way to get information into a function is by passing it as an argument, and the preferred way to get it out is with a return
statement. This keeps your functions self-contained, predictable, and reusable.
What’s Next?
You now understand that the global scope is the top level of your script and that functions can read from it. You also know that assigning to a name inside a function creates a new local variable by default, even if a global variable with the same name exists.
So, if assigning to a name inside a function just creates a local variable, is it even possible to modify a global variable from within a function? Yes, it is, but you have to be explicit about it. In Post #97, we will learn how to use the global
keyword to do just that.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com