Scope Resolution in Python | LEGB Rule

Last Updated : 27 Jul, 2026

Scope resolution in Python determines how variable names are searched and resolved during program execution. Python follows the LEGB rule, which searches for names in the Local, Enclosed, Global, and Built-in scopes in that order. Understanding this rule helps avoid naming conflicts and makes it easier to work with functions, nested functions, and global variables.

Namespaces

A namespace is a container that maps names (identifiers) to Python objects such as variables, functions, classes, and modules. It helps organize names and prevents conflicts by allowing the same identifier to exist in different parts of a program without interfering with one another. Python automatically creates namespaces for modules, functions, classes, and built-in names.

Python
x = 10
def display():
    x = 20
    print("Inside function:", x)


display()
print("Outside function:", x)

Output
Inside function: 20
Outside function: 10

Explanation:

  • The global namespace contains the variable x = 10.
  • When display() is called, Python creates a new local namespace for the function.
  • The variable x = 20 exists only within the function's local namespace and temporarily hides the global variable with the same name.
  • After the function finishes execution, the local namespace is destroyed, and the global variable remains unchanged.

Note: A namespace stores the mapping between names and objects, whereas a scope determines where Python searches for those names during program execution.

Understanding Scope 

A scope defines the region of a program where a name (variable, function, or object) is accessible. When a name is referenced, Python searches for it according to its scope. This search process is known as scope resolution.

Example: The following example demonstrates how variables with the same name can exist in different scopes.

Python
message = "Global variable" 

def display(): 
    message = "Local variable" 
    print(message) 
    
display() 
print(message)

Output
Local variable
Global variable

Explanation:

  •  The variable message inside display() belongs to the function's local scope.
  • The variable message outside the function belongs to the global scope.
  • When display() is executed, Python first searches for message in the local scope and prints "Local variable".
  • Outside the function, Python accesses the global variable and prints "Global variable".

Relationship Between Namespace and Scope

Namespace

Scope

Stores names and their associated objects.

Determines where Python searches for those names.

Created for modules, functions, classes, and built-in names.

Determines the visibility and accessibility of names.

Organizes identifiers to avoid naming conflicts.

Follows the LEGB (Local, Enclosed, Global, Built-in) rule for name resolution.

Understanding the LEGB Rule in Python

Python uses the LEGB rule to determine where to look for a variable or function name when it is referenced. It searches namespaces in a fixed order and stops as soon as it finds a matching name. LEGB stands for:

  • Local (L): Names defined inside the current function.
  • Enclosed (E): Names defined in the enclosing function of a nested function.
  • Global (G): Names defined at the module (file) level.
  • Built-in (B): Names provided by Python's built-in namespace, such as print(), len(), and max().
Scope Resolution in Python

Local Scope in Python

Local scope refers to variables defined in the current function. Always, a function will first look up a variable name in its local scope. Only if it does not find it there, the outer scopes are checked. 

Python
message = "Global"
def display():
    message = "Local"
    print(message)

display()

Output
Local

Explanation:

  • message inside display() belongs to the local scope.
  • Python searches the local scope first, so the local variable is printed.

Enclosed Scope in Python

Enclosed scope refers to the scope of an enclosing function in nested functions. If a variable is not found in the local scope, Python searches the enclosing function before moving to the global scope.

Python
def outer():
    message = "Enclosed"

    def inner():
        print(message)

    inner()

outer()

Output
Enclosed

Explanation:

  • message is defined in the enclosing function outer().
  • Since it is not available in inner(), Python searches the enclosed scope and prints "Enclosed".

Note: Use the nonlocal keyword if you want to modify a variable defined in the enclosing scope.

Global Scope in Python

Global scope contains variables defined outside all functions. These variables are accessible throughout the module and can be read inside functions unless a local variable with the same name exists:

Python
message = "Global"

def display():
    print(message)

display()

Output
Global

Explanation:

  • global count tells Python to use the variable from the global scope.
  • Without the global keyword, assigning to count inside the function would create a new local variable.

Built-in Scope

The built-in scope contains names that are predefined by Python, such as built-in functions, exceptions, and constants. If a name is not found in the local, enclosed, or global scopes, Python searches the built-in scope.

Python
numbers = [4, 8, 2]

print(len(numbers))
print(max(numbers))

Output
3
8

Explanation:

  • len() returns the number of elements in the list.
  • max() returns the largest element in the list.
  • Since these functions are built into Python, they are found in the built-in scope.
Comment