Introduction: Variable Scope Concepts in Python
Some variable scope concepts in Python apply to more than one topic, such as local variables, global variables, and the global keyword. These shared concepts are collected on this page so the same explanation does not need to be repeated across multiple tutorials.
Quick Navigation
Local and Global Variables With the Same Name
It is one of the important variable scope concepts in Python. This concept often confuses beginners when a local variable and a global variable have the same name. Knowing how Python handles this situation helps avoid unexpected results when working with variables inside and outside functions. Let’s explore this concept in detail.
Rule: When this happens, the local variable takes priority inside the function.
message = "Global"
def show_message():
message = "Local"
print(message)
show_message()
print(message)
# Output:
Local
Global
Explanation: Inside show_message(), Python uses the local message variable. Outside the function, Python uses the global message variable.
The local variable does not change the value of the global variable simply because both variables have the same name.
Local vs Global Scope in Python
A clear understanding of the difference between local and global scope is essential for mastering variable scope concepts in Python. Let’s see where variables in each scope can be accessed.
| Feature | Local Scope | Global Scope |
|---|---|---|
| Where the Variable Is Created | Inside a function | Outside functions |
| Where It Can Be Accessed | Inside the function where it is defined | From other parts of the program when available |
| Accessed Inside a Function | Yes | Yes |
| Accessed Outside the Function | No | Yes |
| Lifetime | Generally exists while the function is executing | Generally exists while the program is running |
Needs global When Assigned Inside a Function |
No | Yes, when modifying the global variable from inside a function |
Accessing Variables From Different Scopes
Knowing where a variable can be accessed is an important part of variable scope concepts in Python. A function can access a global variable, while a local variable is available only within that function. Let’s understand this with an example.
language = "Python"
def show_language():
version = "3.14"
print(language)
print(version)
show_language()
# Output:
Python
3.14
Explanation:
languageis a global variable, soshow_language()can access its value.versionis a local variable, so it can be accessed only insideshow_language().- Code outside
show_language()cannot directly access the localversionvariable.
Let’s see what happens when a local variable is accessed outside its function.
Accessing a Local Variable Outside Its Function
What happens if we try to access a local variable outside its function?
def show_version():
version = "3.14"
print(version)
show_version()
print(version)
# Error:
NameError
Explanation:
- The
versionvariable is created insideshow_version(), so it is a local variable. - It can be accessed inside the
show_version()function. - Trying to access the
versionvariable outside the function raises aNameError.
nonlocal vs global
Both nonlocal and global are important variable scope concepts in Python that allow a function to modify a variable outside its local scope. However, they refer to different scopes.
| Feature | global |
nonlocal |
|---|---|---|
| Refers To | The global scope | The nearest enclosing function’s scope |
| Used Inside | A function | A nested function |
| Variable Must Exist In | The global scope | An enclosing function scope |
| Can Modify a Global Variable | Yes | No |
| Can Modify an Enclosing Function Variable | No | Yes |
| Works Across Nested Function Scopes | Yes, for a global variable | Yes, for a variable in an enclosing function |
Let’s see how global affects variables in different scopes with an example.
Example
count = 0
def outer():
count = 10
def change():
global count
count = 20
change()
print(count)
outer()
print(count)
# Output:
10
20
Explanation:
global countrefers to the globalcount, so its value changes from0to20.- The
countinsideouter()remains10because it is a separate local variable. - This shows that
globalmodifies the global variable, not the variable belonging to the enclosing function.
Key Takeaways: Python variable scopes
Here are the main points to keep in mind when working with variable scopes in Python and related keywords.
- A local variable and a global variable can have the same name.
- When the same name exists in local and global scopes, the local variable is used inside the function.
- Local and global scope have different access boundaries.
- The
globalkeyword refers to the global scope. - The
nonlocalkeyword refers to a variable in an enclosing function scope. - Shared scope concepts are kept on this page to avoid unnecessary duplication across the dedicated tutorials.