Computer Science · Ch 7 — Functions
Scope of a Variable
Scope of a Variable
A variable defined inside a function cannot be used outside it. Every variable in a program has a well-defined region where it is accessible, and the part of the program in which a variable can be accessed is called the scope of that variable.
A variable can have one of two scopes (see Figure 7.6):
- global scope — such a variable is a global variable;
- local scope — such a variable is a local variable.
(A) Global variables
In Python, a variable defined outside any function or block is a global variable. It can be accessed in any function defined after it in the program. Because all those functions see the same variable, any change made to a global variable affects every function in the program where the variable is accessible.
(B) Local variables
A variable defined inside a function or a block is a local variable. It can be accessed only within the function or block where it is defined, and it exists only while that function is executing — once the function ends, the local variable is gone.
Seeing the difference in a program
The following program creates one global variable (num) and one local variable (y, inside the function), then tries to access both inside and outside the function:
# Accessing variables inside and outside a function
num = 5 # global variable
def myFunc1():
y = num + 5 # y is LOCAL to myFunc1
print("Accessing num -> (global) in myFunc1, value =", num)
print("Accessing y -> (local variable of myFunc1) accessible, value =", y)
myFunc1()
print("Accessing num outside myFunc1", num)
print("Accessing y outside myFunc1", y) # ERROR — y no longer exists
Accessing num -> (global) in myFunc1, value = 5
Accessing y -> (local variable of myFunc1) accessible, value = 10
Accessing num outside myFunc1 5
Traceback (most recent call last):
File "Prog.py", line 9, in <module>
print("Accessing y outside myFunc1", y)
NameError: name 'y' is not defined
The global num is readable both inside and outside the function, and the local y works fine inside — but the moment y is accessed outside myFunc1(), Python raises a NameError, because a local variable does not exist beyond its function.
Points to note
- Any modification to a global variable is permanent and affects all the functions in which that variable is used.
- If a variable with the same name as a global variable is defined inside a function, it is treated as a local variable of that function and it hides the global variable there.
- If a function needs to modify a global variable so that the modified value is visible outside the function, the variable name must be prefixed with the keyword
globalinside the function.
Using the global keyword
# Modifying a global variable inside a function
num = 5
def myfunc1():
# 'global' tells Python to use (and update) the global num,
# so the change is visible outside the function too
global num …
Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.
Figure 7.6 is a small tree diagram of three boxes summarising the classification introduced in Section 7.4. At the top sits a box labelled Variable Scope, connected downward to two boxes side by side: Global Scope and Local Scope.
The diagram states the section's fundamental fact: every variable in a Python program has exactly one of two possible scopes — there is no third kind.
- A variable with global scope (a global variable) is one defined outside any function or block; it can be accessed in any function defined after it, and a change to it affects every function that uses it.
- A variable with local scope (a local variable) is one defined inside a function or block; it is accessible only there and exists only while that function is executing. …