Python: Variable Scope
Python Variable Scope
python
variables
scope
Python Variable Scope
1 Overview
📘 Variable Scope
Variable scope in Python determines where a variable can be used within a program, and understanding this is key for writing clear, bug-free code. Variable scope in Python—a fundamental idea for understanding how your variables behave. In simple terms, the scope of a variable is the part of the program where you can access and use that variable. There are a few main types to know:
- Local scope: Variables defined inside a function; only accessible within that function.
- Global scope: Variables defined outside any function; accessible throughout the module.
- Nonlocal scope: Variables defined in an enclosing function (not global, not local); accessible from nested (inner) functions using the nonlocal keyword.
2 Variable Scope
Variable scope in Python determines where a variable can be used within a program, and understanding this is key for writing clear, bug-free code. Python has four primary scope types: local, global, enclosing, and built-in.
3 Main Types of Variable Scope
3.1 Built-in
- Python’s built-in functions and modules.
- E.g., `print()`, `len()`, or `range()`.
- These functions are always available everywhere.
Hello, World!
3.2 Global scope
- Variables defined outside functions or classes.
- Accessible from any function within the program.
I am a global variable
I am a global variable
3.3 Local scope
- Variables defined within a function.
- Accessible only within the function.
I am a local variable
- Trying to access `local_var` outside the function gives an error:
NameError: name ‘local_var’ is not defined
3.4 Enclosing scope
- Exists when a function is nested within another function.
- The inner function can access the variables of the outer function.
- The outer function cannot access the inner function’s variables.
def outer_function():
enclosing_var = "I am in the enclosing scope"
def inner_function():
# Accessing the enclosing scope variable
print(enclosing_var)
local_var = "I am in the local scope"
print(local_var)
inner_function()
# Cannot access inner_function's variables --> Error
# print(local_var)
outer_function()
I am in the enclosing scope
I am in the local scope
I am in the local scope
4 Shadowing
- The local variable “shadows” the global variable when they have the same name.
- The local variable takes precedence.
- The global variable is not accessible within the function.
I am a local variable
I am a global variable
I am a global variable
5 Loops and conditionals
- Variables defined within an `if`, `for` or `while` block have the same scope as the surrounding context.
- If the `if` statement is in the global scope, the variable will have global scope.
- If the `if` statement is within a function, the variable will have local scope.
- This is the case for Python, but not in other programming languages.
- E.g., in C++, Java, Rust, or Go, variables declared inside an `if`/`for`/`while` block are not accessible outside of it.
Positive