Ad – 728×90
⚙️ Functions

Python Scope – Local, Global, Enclosing, and Built-in (LEGB Rule)

Scope determines where a variable is visible and accessible in your code. Python follows the LEGB rule to look up variable names: Local → Enclosing → Global → Built-in. Misunderstanding scope causes some of the most confusing bugs in Python.

⏱️ 20 min read 🎯 Intermediate 📅 Updated 2026

The LEGB Rule

When Python encounters a variable name, it searches in this order: Local (inside the current function), Enclosing (outer function in nested functions), Global (module-level), Built-in (Python's built-in names like len, print, range).

Python
x = "global"     # G - global scope

def outer():
    x = "enclosing"  # E - enclosing scope
    def inner():
        x = "local"  # L - local scope
        print(x)     # Finds "local" first
    inner()
    print(x)         # Finds "enclosing"

outer()
print(x)             # Finds "global"
▶ Output
local enclosing global

The global Keyword

To modify a global variable inside a function, declare it with the global keyword.

Python
count = 0  # Global

def increment():
    global count   # Tell Python to use the global count
    count += 1

increment()
increment()
increment()
print(count)  # 3 - global was modified
▶ Output
3
💡
Tip

Using global is generally discouraged. Prefer passing values as arguments and returning them instead.

Ad – 336×280

The nonlocal Keyword (Closures)

In nested functions, nonlocal refers to the enclosing function's variable (not global).

Python
def make_counter():
    count = 0
    def counter():
        nonlocal count   # Refers to outer function's count
        count += 1
        return count
    return counter

my_counter = make_counter()
print(my_counter())  # 1
print(my_counter())  # 2
print(my_counter())  # 3
▶ Output
1 2 3

Built-in Scope

Built-in scope contains Python's built-in functions and constants. Be careful not to shadow them with your own variable names.

Python
# ❌ Don't shadow built-ins!
list = [1, 2, 3]   # Overwrites the built-in list!
print = "hi"       # Overwrites print! Now you can't print!

# ✅ Use descriptive names
my_list = [1, 2, 3]
message = "hi"
print(message)     # Works fine