
Unit 3 Lab: Variable Scope
Lesson Objectives
After this lesson, you will be able to…
- Define variable scope.
- Use the global keyword to access global variables.
- Explain the order of scope precedence that Python follows when resolving variable names.
Discussion: Delivering a Letter
What if someone wanted to send Brandi a letter?
If you just had “For Brandi,” the mail carrier would give the letter to the first Brandi they see!
They’d look:
- First in the class. Is there a “Brandi” here? They get the letter!
- No? OK, look in the town. Is there a “Brandi” here? They get the letter!
- No? OK, look in the state. Is there a “Brandi” here? They get the letter!
Discussion: Your Address
That’s why scope matters. We might have to get more specific. To correctly deliver the letter, if the mail carrier only looked in the scope of:
Your class:
- You’re probably the only Brandi.
- “For Brandi” is fine.
Your town:
- There might be multiple Brandis in the town.
- “For Brandi, on Main Street” is a bit more specific.
In your state:
- There are multiple Main Streets in New York!
- “For Brandi, on Main Street in Brooklyn” is more specific.
Discussion: What Is x?
Python has scope, too. We can have many variables with the same name, and Python will look for the most specific one.
In different scopes, you can reuse the same name. Each one is a completely different variable.
Functions and classes create individual local scopes. A local variable doesn’t exist outside its local function or class scope.
Global Scope
Variables that are in global scope can be accessed anywhere. - Python will check for a local variable before using a global one.
Multiple Variables, One Name
Use case: x and y are frequently used to represent numbers.
Scope is important so they don’t interact!
We Do: Accessing Scopes
Let’s start with global scope:
We Do: Accessing Local Scope
What if we add a variable in a local function scope and try to access it from the global scope?
It fails!
Scope Can Be Tricky
What do you think happened here?
I Do: The Global Keyword
You can call a global variable intentionally with global. * What do you think happens if you forget the global keyword?
We Do: Global vs. Local
In the following code, there are three print statements. Before you run the code, guess what those print statements will print.
You Do: Just a Day in the Jungle
Open a new local file, piranhas.py.
- Declare a global variable
piranhas_hungry and set it to True.
- Write two functions,
swing_vine_over_river and jump_in_river.
- In
swing_vine_over_river, print Ahhh! Piranhas got me!.
- Change
piranhas_hungry to False.
- In
jump_in_river, if piranhas_hungry is True, print I'm not going in there! There are hungry piranhas!.
- Otherwise, print
Piranhas are full! Swimming happily through the Amazon!
Pro tip: Raise your hand if you need some help!
We Do: Check Your Answers
- Did you remember the
global keyword?
- What happens if that keyword is removed?
- Comment out line 4. What happens? Why?
Summary and Q&A
Python checks scope to find the right variable.
- Functions and classes create individual local scopes.
- A
local variable doesn’t exist outside its local function or class scope.
- Any variable declared or assigned outside of any function or class is considered “global.”
- Variables that are in global scope can be accessed anywhere.
Python will check for a local variable before using a global one.
There can be more levels. Python always works from the inside out — keep that in mind as your programs get more advanced!