Lokang 

Python and MySQL

Scope of Variables

The scope of a variable refers to the region of the code where a variable is accessible or can be used. In Python, the scope of a variable is determined by where the variable is declared. There are four main types of scope:

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

1. Local Scope

  • Variables that are declared inside a function or a block are said to have a local scope. They can only be accessed within that function or block.
  • Once the function terminates, the local variables are destroyed and cannot be accessed outside the function.
def my_function():
   local_var = 10  # Local scope
   print(local_var)  # Accessible inside the function
my_function()
print(local_var)  # Error: local_var is not defined

2. Enclosing Scope

  • This scope is specific to nested functions. When a function is defined inside another function, the inner function has access to variables from the enclosing (outer) function’s scope.
  • This is also referred to as non-local scope.
def outer_function():
   outer_var = "I am outer"
   def inner_function():
       print(outer_var)  # Accessible in the inner function
   inner_function()
outer_function()

3. Global Scope

  • Variables that are declared outside of all functions and classes have a global scope. They can be accessed from anywhere in the code, both inside and outside functions.
  • However, to modify a global variable inside a function, you need to explicitly declare it as global.
global_var = "I am global"
def my_function():
   print(global_var)  # Accessible inside the function
my_function()
print(global_var)  # Accessible outside the function as well
def modify_global():
   global global_var
   global_var = "Modified global"
modify_global()
print(global_var)  # Output: Modified global

4. Built-in Scope

  • Python provides a set of built-in functions and variables that are always available, regardless of what you name your variables. This is known as the built-in scope.
  • Examples of built-in functions include len(), print(), and type().
# The `print` and `len` functions are examples of built-in scope.
print(len("Hello"))  # Output: 5

LEGB Rule

Python resolves the scope of variables using the LEGB rule, which stands for:

  • Local: The innermost scope, which is the local scope within a function.
  • Enclosing: The scope of any enclosing functions, when dealing with nested functions.
  • Global: The scope of the module or script itself, where global variables reside.
  • Built-in: The outermost scope, containing built-in functions and exceptions.

When Python encounters a variable, it checks these scopes in order: first the local scope, then enclosing, then global, and finally the built-in scope. It uses the first match it finds.

Example to Demonstrate Scope

x = "global"
def outer_function():
   x = "enclosing"
   def inner_function():
       x = "local"
       print("Inner:", x)  # Local scope
   inner_function()
   print("Outer:", x)  # Enclosing scope
outer_function()
print("Global:", x)  # Global scope

Output:

Inner: local
Outer: enclosing
Global: global

Global vs. Local Scope Example

If you try to modify a global variable inside a function without declaring it as global, Python will treat it as a local variable, which can lead to unexpected results or errors.

x = 10
def my_function():
   x = x + 5  # Error: UnboundLocalError because x is treated as a local variable
my_function()

To modify the global x, you need to declare it as global:

x = 10
def my_function():
   global x
   x = x + 5  # Now this works, and x is modified globally
my_function()
print(x)  # Output: 15

Summary

Understanding the scope of variables is crucial for managing the accessibility and lifetime of variables in your Python programs. It helps prevent bugs related to variable shadowing and ensures that your code is more predictable and easier to debug.