Python Scopes & LEGB — Rules Guide

Python Scopes & LEGB — Rules Guide

The concept of scoping in Python allows for greater control over elements in your code, which reduces the chances of accidental or unwanted changes.

Before you explore scope and its intricacies, it’s important to know that Python has four different types of scope.

The Python scope concept is generally presented using a rule known as the LEGB (Local -> Enclosing -> Global -> Built-in) rule.

The purpose of scope is to protect the variable, so it does not get changed by other parts of the code.

Variables within the built-in and global scope are accessible from anywhere in the code.

1. Local scope

Local scope refers to a variable declared inside a function. For example, in the code below, the variable total is only available to the code within the calculate_average function. Anything outside of this function will not have access to it.

def calculate_average(numbers):
    # Local variable declared inside a function
    total = sum(numbers)
    average = total / len(numbers)
    return average

# Example usage of the function
data = [10, 20, 30, 40, 50]
print("Average:", calculate_average(data))

# Attempting to access local variable 'total' outside of the function
print("Total:", total)

Here, we define a function calculate_average that takes a list of numbers as input. Inside the function, we calculate the total of the numbers using the sum() function and then compute the average by dividing the total by the number of elements in the list.

The variables total and average are declared inside the calculate_average function, making them local variables. They are only accessible within the function's body.

When we call the calculate_average function with the data list [10, 20, 30, 40, 50], it computes the average of these numbers and prints it.

However, when we attempt to access the local variable total outside of the function, Python raises a NameError

It exists only within the calculate_average function's local scope.

2. Enclosing scope

Enclosing scope refers to a function inside another function or what is commonly called a nested function.

def calculate_average(numbers):
    # Local variable declared inside a function
    total = sum(numbers)
    average = total / len(numbers)

    # Nested function accessing 'total'
    def display_total():
        print("Total inside function:", total)

    # Call the nested function
    display_total()

    return average

# Example usage of the function
data = [10, 20, 30, 40, 50]
average_result = calculate_average(data)
print("Average:", average_result)

# Attempting to access local variable 'total' outside of the function
# This will result in a NameError
print("Total outside function:", total)

The display_total() function is nested within the calculate_average() function, making it an example of an enclosed scope. It has access to the variables defined in the outer function, including total.

When we call display_total() inside calculate_average(), it prints the value of total. This demonstrates how the nested function can access variables from its enclosing scope.

However, attempting to access the local variable total from outside the function, as shown in the commented-out line, would result in a NameError.

3. Global scope

Global scope is when a variable is declared outside of a function. This means it can be accessed from anywhere.

In the code below, I added a global variable called total_global. This can then be accessed from both functions calculate_average()and display_total()

total_global = 0  # Global variable

def calculate_average(numbers):
    # Local variable declared inside a function
    total = sum(numbers)
    average = total / len(numbers)
    print(total_global)

    # Nested function accessing 'total'
    def display_total():
        print("Total inside function:", total)
        print(total_global)

    # Call the nested function
    display_total()

    return average

The nested items have access to both the global and the enclosed, but from the outside, it can’t be accessed from a nested or an enclosed scope, both the local and enclosed.

Global Keyword

When a variable is declared within a function in Python, it typically belongs to the local scope of that function. However, the global keyword allows developers to explicitly designate a variable as belonging to the global scope, making it accessible from anywhere within the codebase.

total_sales = 0  # Global variable

def record_sale(employee_name, amount):
    global total_sales  # Declare total_sales as global

    # Update total sales with the amount of the current sale
    total_sales += amount

    # Print a message indicating the sale recorded for the employee
    print(f"Sale recorded for {employee_name}: ${amount}")
    print(f"Total sales: ${total_sales}")

# Simulate sales made by different employees
record_sale("Alice", 100)
record_sale("Bob", 150)
record_sale("Charlie", 200)

Note: The use of global is considered bad practice in general. Aim to keep functions self-contained and minimise unintended interactions with global state to maintain code predictability and reliability.

4. Built-in scope

Built-in scope refers to the reserved keywords that Python uses for its built-in functions, such as print, def, for, in, and so forth. Functions with built-in scope can be accessed at any level.

# Built-in function 'print'
print("Hello, World!") 

# Built-in function 'range'
for i in range(5):
    print("Iteration", i+1)

# Define a function named 'add_numbers' that takes two numbers as input and returns their sum
def add_numbers(a, b):
    return a + b

result = add_numbers(5, 7)
print("Result of addition:", result)

Built-in scope covers all the language of Python, which means you can access it from the outermost scopes or the innermost scopes in the function classes.

Conclusion:

With this knowledge at hand, you can take advantage of Python scopes to write more reliable and maintainable programs.

If you found this article insightful, stay tuned for more! I’ll be diving deeper into various aspects of Python programming in future articles.

Did you find this article valuable?

Support Insha's Blog by becoming a sponsor. Any amount is appreciated!