1. Local Scope
def show_value():
x = 10 # Local variable
print(x)
show_value()
# print(x) # NameError: x is not defined
Local variables exist only within the function where they are declared.
2. Global Scope
x = 20 # Global variable
def display():
print(x)
display()
print(x)
Global variables are accessible throughout the module.
3. Local vs Global Variable Conflict
Local variables override global variables within function scope.
4. Using the global Keyword
The global keyword allows modification of global variables inside functions.
5. Enclosing Scope (Nested Functions)
Nested functions can access variables from their enclosing scope.
6. Using the nonlocal Keyword
nonlocal allows modification of variables from the enclosing (but not global) scope.
7. LEGB Rule (Scope Resolution Order)
Python resolves variables using the LEGB rule:
8. Built-in Scope
Built-in functions and exceptions reside in the built-in scope.
9. Scope Inside Loops
Unlike some languages, Python loop variables are not block-scoped.
10. Practical Example of Scope Management
Demonstrates coordinated use of local, enclosing, and global scopes.
78. Python Variable Scope — Local, Global, and Nonlocal (Comprehensive Guide)
1. Concept Overview
Variable scope defines where a variable is accessible and modifiable within a Python program.
Python resolves variable names using the LEGB rule:
Local – inside the current function
Enclosing – inside outer (nested) functions
Global – defined at module level
Built-in – predefined by Python
Understanding scope prevents unpredictable behavior and subtle bugs.
Variables declared inside a function are local by default.
Characteristics:
Exists only within the function
Destroyed after function execution
Safest and most predictable scope
3. Global Scope
Variables declared outside any function are global.
Characteristics:
Accessible throughout the module
Persists during program execution
Risk of unintended modification
4. Modifying Global Variables (global keyword)
Without global, Python treats variables as local.
Use case:
5. Enclosing Scope (Nested Functions)
The inner function can access variables from the enclosing function.
6. Modifying Enclosing Variables (nonlocal keyword)
nonlocal allows nested functions to update variables from their enclosing scope.
7. LEGB Resolution Demonstration
Resolution order:
Local → Enclosing → Global → Built-in
8. Shadowing Variables
Here, the local value hides the global value.
⚠️ Shadowing can cause confusion and unexpected behavior.
9. Built-in Scope
Python provides built-in identifiers:
Avoid overwriting:
This breaks core functionality.
10. Scope Behavior in Conditional Blocks
Python does not support block-level scope — variables exist at function level.
Scope Hierarchy Summary
Scope Type
Where It Exists
Keyword Control
Variable Lifetime with Scope
During function execution
As long as closure exists
Real-World Enterprise Example
Demonstrates:
Closure-powered persistence
Common Mistakes
Forgetting global when updating variables
Shadowing variables unintentionally
Redefining built-in identifiers
Poor Scope Practice
Impact
Minimize global scope usage
Use nonlocal for closure state only
Avoid variable name reuse
Use function parameters instead of globals
Enterprise Importance
Mastery of variable scope ensures:
Clean modular architecture
Critical for:
Comparison: Local vs Global vs Nonlocal
Feature
Local
Global
Nonlocal
Architectural Value
Correct scope handling enables:
Clean dependency management
Predictable execution flow
Last updated