Python Scope

Python Scope refers to the region in a program where a variable is defined, accessible and can be used. In Python, there are four types of scope: Local, Enclosing, Global, and Built-in. Understanding the concept of scope is important in programming as it determines the accessibility and visibility of a variable. In this blog, we will discuss the different types of scopes in Python and how to use them with code snippets.

Local Scope

Local scope refers to the area within a function where a variable is defined. This means that a variable defined inside a function cannot be accessed outside that function. This is because the variable only exists within the function where it is defined. Here is an example:

def my_func():
    x = 5 # local variable
    print(x)

my_func()
print(x) # NameError: name 'x' is not defined

In the above example, x is a local variable defined inside the function my_func(). The print() function can access and print the value of x inside the function, but outside the function, it does not exist. The print(x) statement raises a NameError because x is not defined in the global scope.

Enclosing Scope

Enclosing scope refers to the area within nested functions where a variable is defined. In Python, a function can be defined inside another function. The enclosing function is the outer function, and the nested function is the inner function. A variable defined in the outer function is accessible inside the inner function. Here is an example:

def outer_func():
    y = 10 # enclosing variable
    def inner_func():
        print(y) # accessing enclosing variable
    inner_func()

outer_func() # Output: 10

In the above example, y is an enclosing variable defined in the outer function outer_func(). The inner function inner_func() can access the value of y using the print(y) statement. When we call outer_func(), it invokes inner_func(), which prints the value of y.

Global Scope

Global scope refers to the area outside all functions where a variable is defined. This means that a variable defined in the global scope can be accessed from any part of the program. Here is an example:

x = 5 # global variable

def my_func():
    print(x)

my_func() # Output: 5
print(x)  # Output: 5

In the above example, x is a global variable defined outside the function my_func(). The function can access the value of x using the print(x) statement. The print(x) statement outside the function also prints the value of x.

Built-in Scope

Built-in scope refers to the area where the pre-defined Python functions and modules are defined. These are the functions and modules that are included in the Python programming language by default. Here is an example:

import math

def my_func():
    print(math.pi)

my_func() # Output: 3.141592653589793

In the above example, math.pi is a built-in variable defined in the math module. The function my_func() can access the value of math.pi using the print(math.pi) statement.

the concepts of scope, we can write more effective and efficient Python code.

Scope Resolution

When a variable is accessed, Python searches for the variable in the following order:

  1. Local scope
  2. Enclosing scope
  3. Global scope
  4. Built-in scope

This order of searching is called the “Scope Resolution Order.” If the variable is not found in any of the above scopes, a NameError is raised.

def my_func():
    print(x)

my_func() # Output: NameError: name 'x' is not defined

In the above example, x is not defined in any of the scopes, so a NameError is raised.

Modifying Variables with Global Keyword

Although using global variables is not considered good programming practice, there may be situations where you need to modify a global variable from within a function. In Python, you can use the global keyword to declare a variable as global and then modify it within a function. Here is an example:

x = 5 # global variable

def my_func():
    global x
    x = 10 # modifying global variable
    print(x)

my_func() # Output: 10
print(x)  # Output: 10

In the above example, we declare x as a global variable using the global keyword. Then, we modify the value of x to 10 inside the function my_func(). When we call the function, it prints the modified value of x. The print(x) statement outside the function also prints the modified value of x.

Conclusion

In this blog, we have discussed the different types of Python scope, how to access variables from different scopes, and how to modify global variables from within a function. Understanding scope is important for writing efficient, maintainable, and error-free Python code. By using the different types of scope effectively, we can prevent errors and improve the readability and maintainability of our code.