avatarLaxfed Paulacy

Summary

The provided web content discusses the concept of scope in Python, explaining the Local, Enclosing, and Global scopes according to the LEGB rule.

Abstract

The article on the undefined website delves into the intricacies of scope in Python programming. It emphasizes the importance of understanding the Local, Enclosing, and Global scopes, which are part of the LEGB (Local, Enclosing, Global, Built-in) rule. The local scope pertains to names defined within a function and is temporary, existing only during the function's execution. The enclosing scope is relevant for nested functions, allowing inner functions to access variables from the outer function's scope. The global scope, on the other hand, is the top-level scope where variables are accessible from anywhere in the code. The article uses code examples to illustrate how these scopes operate and how they maintain separate values for the same variable name, which is crucial for writing effective and maintainable Python code.

Opinions

  • The article implies that a clear understanding of Python's scope rules is essential for writing maintainable and efficient code.
  • It suggests that the LEGB rule is a fundamental concept that every Python programmer should master to manage variable names effectively.
  • The use of nested functions and the concept of enclosing scope are presented as powerful features of Python for structuring code.
  • The article's author seems to advocate for a thoughtful approach to variable scoping to avoid common errors, such as trying to access a local variable from the global scope.
  • The inclusion of practical examples indicates that hands-on learning and understanding through application are valued methods for grasping Python's scope concepts.

PYTHON — Local Enclosing Global Scope in Python

Technology should improve your life… not become your life. — Anonymous

PYTHON — Using Terminal Windows Overview in Python

When working with Python, understanding scope is essential for writing effective and maintainable code. Python utilizes the LEGB rule, which stands for Local, Enclosing, Global, and Built-in scopes. In this tutorial, we’ll focus on exploring the first three scopes: local, enclosing, and global.

Local Scope

The local scope contains names defined inside a function. Each time a function is called, a new local scope is created. Let’s take a look at an example:

def print_total():
    total = 0
    print(f"From function: total={total}")

print_total()  # Output: From function: total=0
print(total)    # Throws an error as total is not defined outside the function

In this example, the variable total is defined within the print_total() function and is accessible only within that function.

Enclosing Scope

The enclosing scope exists for nested functions and is also known as nonlocal scope. Names defined in the enclosing scope are accessible to inner functions. Here’s an example:

def print_total():
    total = 5  # Global scope

    def inner_print_total():
        total = 7  # Enclosing scope
        print(f"From inner function: total={total}")

    inner_print_total()
    print(f"From outer function: total={total}")

print_total()  # Output: From inner function: total=7, From outer function: total=5

In this example, the variable total is defined in both the enclosing and global scopes, and Python distinguishes between them.

Global Scope

The global scope is the top-level scope in a Python program, and names defined in this scope are visible from anywhere in the code. Here’s how the global scope works:

total = 5  # Global scope

def print_total():
    total = 12  # Enclosing scope

    def inner_print_total():
        total = 7  # Local scope
        print(f"From inner function: total={total}")

    inner_print_total()
    print(f"From outer function: total={total}")

print_total()  # Output: From inner function: total=7, From outer function: total=12
print(total)    # Output: 5

In this example, the variable total is defined in the global, enclosing, and local scopes, and each one maintains its own separate value.

By understanding these three scopes, you can effectively organize and manage variable names in your Python code. In the next lesson, we’ll explore the built-in scope and complete our understanding of the LEGB rule.

In conclusion, understanding the local, enclosing, and global scopes in Python is crucial for writing efficient and well-structured code. By grasping the concept of scope, you can effectively manage variable names in your Python programs. Understanding these scopes is fundamental to writing code that is both clear and maintainable.

PYTHON — Summary of Numpy arange in Python

Global
Enclosing
Python
ChatGPT
Local
Recommended from ReadMedium