Lesson 24 of 30 · Functions
Variable Scope
Understand where Python variables can be used. Learn local and global scope, why a function cannot see another function's variables and how to share data properly.
- 7 min read
- Beginner
- Reviewed
- What scope means
- The difference between local and global variables
- Why variables inside a function disappear when it ends
- The right way to move data in and out of functions
Where does a variable live?
Not every variable can be used everywhere in a program. The part of a program where a variable can be used is called its scope.
There are two kinds you need to know.
- A local variable is created inside a function. It exists only while that function runs, and only that function can use it.
- A global variable is created outside all functions. It can be read from anywhere in the file.
Local variables stay inside
def make_greeting():
message = "Hello from inside the function"
print(message)
make_greeting()
print(message)The first print works. The second raises NameError: name 'message' is not defined. The variable message was created inside the function, and it vanished when the function ended.
Think of a function as a room with one-way glass. Code inside can see out, but code outside cannot see in.
Functions can read global variables
site_name = "Python for Beginners"
def show_title():
print("Welcome to", site_name)
show_title()site_name is global, so the function can read it.
Same name, different variables
If you assign to a name inside a function, Python creates a new local variable. A global variable with the same name is left untouched.
score = 100
def reset_score():
score = 0
print("Inside the function:", score)
reset_score()
print("Outside the function:", score)Many beginners expect the last line to print 0. It prints 100. The score inside the function is a different variable that happens to share the name.
Parameters are local too
A parameter behaves exactly like a local variable. Changing it inside the function does not change the caller's variable.
def add_bonus(points):
points = points + 50
print("Inside:", points)
my_points = 10
add_bonus(my_points)
print("Outside:", my_points)The right way: pass in, return out
So how should a function change something? Take the value in as a parameter and hand the new value back with return.
def add_bonus(points):
return points + 50
my_points = 10
my_points = add_bonus(my_points)
print(my_points)This style makes a function predictable. Everything it needs comes in through its parameters, and everything it produces goes out through return. You can understand it without reading the rest of the program.
The global keyword
Python has a keyword, global, that lets a function change a global variable.
visits = 0
def record_visit():
global visits
visits += 1
record_visit()
record_visit()
print(visits)It works, but use it rarely. When many functions change the same global variable, it becomes very hard to work out which one caused a bug.
Common mistakes
Fix the program by returning the result from the function instead of trying to read a local variable from outside.
# This program crashes with a NameError, because "result"
# only exists inside the function. Fix it so the output is: 90
def add_bonus(score):
result = score + 10
add_bonus(80)
print(result)A variable is created inside a function. Where can it be used?
Summary
- Scope is where a variable can be used.
- Local variables live inside a function and disappear when it ends.
- Global variables can be read from anywhere.
- Assigning inside a function creates a local variable, even if a global has the same name.
- Pass values in through parameters and send results out with
return.
You have finished the module on functions. Next: saving information in files.
Finished reading? Tick it off to track your progress.