What is a scope in Python?

Home » Python » What is a scope in Python?

Table of Contents

Introduction to scope in Python

A scope in Python refers to the region of the code where a particular variable or name is accessible or visible. When we refer or modify a variable or a function, it is a scope.

Understanding scope is essential for managing the visibility and lifetime of variables, especially in larger programs where multiple variables might have the same name in different contexts.

Types of Scopes in Python:

Python uses the LEGB rule which is Local, Enclosing, Global and Built-in to determine the order. Which it uses to search for a name when it got a reference in the code.

This rule defines the hierarchy of scopes, and Python will check these scopes in the following order:

  1. Local Scope (L):
    • The innermost scope, which refers to variables defined within the current function or method.
    • If we assign a variable inside a function then it becomes local to that function.
  2. Enclosing Scope (E):
    • Enclosing scope refers to the scope of any enclosing functions. Which means that the function in which the current function nests.
    • If we have a function inside another function, we can consider the variables of the outer function to be in the enclosing scope for the inner function.
  3. Global Scope (G):
    • This refers to variables defined at the top level of a script or module, outside of any function.
    • These variables are accessible throughout the entire module, unless shadowed by a local or enclosing variable.
  4. Built-in Scope (B):
    • This refers to the names that are built into Python (like print()len()int(), etc.).
    • These function are available in all Python scripts by default and user-defined names cannot override them in most cases.

Example of Scope in Python:


# Global scope
x = "Global"

def outer_function():
    # Enclosing scope
    y = "Enclosing"
    
    def inner_function():
        # Local scope
        z = "Local"
        print(x)  # Accessing global variable
        print(y)  # Accessing enclosing variable
        print(z)  # Accessing local variable
    
    inner_function()

outer_function()

Explanation:

  1. Global scope: The variable x is defined in the global scope, so it can be accessed from any function within the same module.
  2. Enclosing scope: The variable y is defined in the outer function and is accessible inside inner_function() because it is in the enclosing scope.
  3. Local scope: The variable z is defined inside inner_function() and is local to that function, so it can be accessed within the function, but not from outside it.

Function Scopes and the LEGB Rule:

When you reference a variable, Python follows the LEGB rule to search for the variable in the following order:

  1. Local: First, we check if the variables are local.
  2. Enclosing: If not fount, It checks the enclosing function’s scope. If one functions nests inside another.
  3. Global: If still not found, it checks the global scope (variables defined at the top level of the script or module).
  4. Built-in: If the variable is not found in any of the above scopes, Python will check the built-in scope for built-in names (such as printlenmin, etc.).

Modifying Variables in Different Scopes:

  • Local Scope: You can modify local variables directly within a function.
  • Global Scope: If you need to modify a global variable inside a function, you need to declare it as global.

Example of Modifying Global Variables:


x = 10  # Global variable

def modify_global():
    global x
    x = 20  # Modifying the global variable

modify_global()
print(x)  # Output: 20

If you don’t use the global keyword, the function will treat x as a local variable and won’t modify the global x.

Conclusion of scope in Python:

  • We use scope to determine where to make variable accessible in Python.
  • The LEGB rule governs the order in which Python searches for a variable: Local, Enclosing, Global, and Built-in.
  • Python’s scoping system prevents naming conflicts. It also isolates the variable names properly.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *