Fundamentals 5 min read

Understanding Python Scope Rules: Global, Local, Enclosing, and Built‑in

This article explains Python's four scope levels—global, local, enclosing (non‑local), and built‑in—describes how the LEGB lookup order works, and provides clear code examples demonstrating variable access and modification in each scope.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Understanding Python Scope Rules: Global, Local, Enclosing, and Built‑in

Python uses a hierarchy of scopes to resolve variable names, which can be divided into four main categories: global scope, local scope, enclosing (non‑local) scope, and built‑in scope.

Global Scope : Variables defined at the top level of a module are visible throughout the entire file. They can be read inside functions, but to modify them from within a function the global keyword must be used.

Local Scope : Variables created inside a function exist only within that function. They are destroyed when the function finishes, and any assignment to a name that is not declared global or nonlocal creates a new local variable.

Enclosing (Non‑Local) Scope : When a function is defined inside another function, the inner function can access variables from the outer (enclosing) function. The nonlocal keyword allows the inner function to modify those variables.

Built‑in Scope : This is the innermost scope that contains Python’s built‑in functions and types such as len() , range() , int , str , etc., which are always available.

The overall lookup follows the LEGB rule: Python searches first in the Local scope, then Enclosing, then Global, and finally Built‑in.

Code examples:

global_var = "I am a global variable."

def func():
    # Local scope
    local_var = "I am a local variable."
    print("Inside function:", local_var)
    print("Accessing global variable inside function:", global_var)

func()  # Output: Inside function: I am a local variable.
# Accessing global variable inside function: I am a global variable.

# Modifying a global variable

def modify_global():
    global global_var
    global_var = "Global variable modified inside function."

modify_global()
print(global_var)  # Output: Global variable modified inside function.
def outer_function():
    outer_var = "I'm in the enclosing scope."
    def inner_function():
        nonlocal outer_var
        outer_var = "Modified from the inner function."
        print("Inner function:", outer_var)
    inner_function()
    print("Outer function after modification:", outer_var)

outer_function()
# Output:
# Inner function: Modified from the inner function.
# Outer function after modification: Modified from the inner function.
def use_builtin():
    print("Length of an empty list using built-in len():", len([]))

use_builtin()  # Output: Length of an empty list using built-in len(): 0

print(type(1))          #
print(type(""))        #
my_list = [1, 2, 3]
print(len(my_list))     # 3
for i in range(5):
    print(i)            # 0 1 2 3 4

def my_function():
    pass
print(type(my_function)) #
Pythoncode examplesVariablesScopeLEGB
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.