Python's Scope of Variable

Explore the scope of variables in Python. Our comprehensive guide covers variable visibility, local and global scopes, and practical examples to master variable scoping in Python.
E
Edtoks3:08 min read

In Python, variable scope refers to the region of a program where a variable is accessible and can be used. Python has several different scopes, which determine where and how a variable can be accessed. The main types of variable scope in Python are:

  1. Local Scope (Function Scope): Variables defined within a function are considered to be in the local scope. They are only accessible within that specific function.

  2. Enclosing Scope (Nonlocal Scope): If a function is defined inside another function, variables from the enclosing function's scope can be accessed by the inner function. This is known as an enclosing or nonlocal scope.

  3. Global Scope: Variables defined at the top level of a module or script are in the global scope. They can be accessed from any part of the module or script.

  4. Built-in Scope: Python has a set of built-in functions and objects that are always available in any Python script or module. These are in the built-in scope.

Now, let's explore variable scope with simple and complex examples:

1. Local Scope (Function Scope)

In this example, the variable x is defined within the my_function function and is only accessible within that function.

def my_function():
    x = 10  # Local variable
    print("Inside my_function:", x)

my_function()
print("Outside my_function:", x)  # This will raise an error because x is not defined in this scope

In this case, x is accessible inside my_function but not outside it.

2. Enclosing Scope (Nonlocal Scope)

If a function is defined inside another function, it can access variables from the enclosing function using the nonlocal keyword.

def outer_function():
    y = 20  # Enclosing variable

    def inner_function():
        nonlocal y
        y = 30
        print("Inside inner_function:", y)

    inner_function()
    print("Inside outer_function:", y)

outer_function()
print("Outside outer_function:", y)  # This will raise an error because y is not defined in this scope

In this example, inner_function can access and modify the y variable from the enclosing outer_function.

3. Global Scope

Variables defined at the top level of a module or script are in the global scope and can be accessed from anywhere in that module or script.

z = 50  # Global variable

def my_function():
    print("Inside my_function:", z)

my_function()
print("Outside my_function:", z)

In this case, z is accessible both inside and outside the my_function because it is in the global scope.

4. Built-in Scope

Python provides a set of built-in functions and objects that are always available. These are in the built-in scope and can be used without importing anything.

result = len([1, 2, 3])  # len() is a built-in function
print("Length of the list:", result)

import math
result = math.sqrt(25)  # math.sqrt() is a function from the math module
print("Square root:", result)

Here, len() and math.sqrt() are examples of functions in the built-in scope. You can use them directly without defining them.

Understanding variable scope is essential for writing clean and maintainable code in Python. It helps prevent naming conflicts and allows you to manage the accessibility of variables and functions within your programs.

 

Let's keep in touch!

Subscribe to keep up with latest updates. We promise not to spam you.