Understanding Python Scopes: Local, Global, Built‑in, and Enclosing
This article explains Python's four scope types—local, global, built‑in, and enclosing—detailing their definitions, lookup rules, and how to access or modify variables within each, supplemented by clear code examples illustrating typical usage and the role of the nonlocal and global keywords.
In Python, a scope determines the accessibility of variables, functions, and other identifiers.
Local Scope: Variables defined inside a function, class, or other code block (such as loops or conditional statements) are visible only within that block. When a name is referenced inside the block, Python first looks for it in the local scope.
Global Scope: Variables defined outside any function or class have global scope and are visible throughout the entire file. If a name is not found in the local scope, Python searches the global scope. To modify a global variable inside a function, the global keyword must be used.
Built‑in Scope: This contains Python’s built‑in names such as len() , print() , int() , etc. It is the outermost scope; if a name cannot be found in local or global scopes, Python checks the built‑in scope.
Enclosing (Nested) Scope: When a function is defined inside another function, the outer function’s local scope becomes the enclosing scope for the inner function. If a name is not found in the local or global scopes, Python continues searching in the enclosing scopes until the global scope is reached.
1. Local Scope Example
def my_function():
local_var = "I am a local variable"
print(local_var)
my_function()
# print(local_var) # This would raise an error because local_var is only defined inside my_function2. Global Scope Example
global_var = "I am a global variable"
def print_global():
print(global_var)
print_global()
print(global_var) # Both can access the global variable
# To modify a global variable inside a function, use the global keyword
def modify_global():
global global_var
global_var = "Global variable has been modified"
modify_global()
print(global_var) # Output: Global variable has been modified3. Built‑in Scope Example
# Using the built‑in len function, which resides in the built‑in scope
my_list = [1, 2, 3]
print(len(my_list)) # Output: 34. Enclosing (Nested) Scope Example
def outer():
outer_var = "I am the outer variable"
def inner():
nonlocal outer_var # Use nonlocal to access and modify the outer variable
outer_var = "Outer variable modified by inner"
print(outer_var)
inner()
print(outer_var) # Prints the modified value because inner changed outer_var
outer_var = "This is the true global variable"
outer()
# Output shows the inner modification reflected in the outer scope
print(outer_var) # Prints the true global variable, unchanged by innerThese examples demonstrate how variables are accessed and modified in local, global, built‑in, and enclosing scopes, highlighting the use of nonlocal for outer (non‑global) variables and global for true global variables.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.