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:
- 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.
- 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.
- 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.
- 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.
- This refers to the names that are built into Python (like
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:
- Global scope: The variable
x
is defined in the global scope, so it can be accessed from any function within the same module. - Enclosing scope: The variable
y
is defined in the outer function and is accessible insideinner_function()
because it is in the enclosing scope. - Local scope: The variable
z
is defined insideinner_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:
- Local: First, we check if the variables are local.
- Enclosing: If not fount, It checks the enclosing function’s scope. If one functions nests inside another.
- Global: If still not found, it checks the global scope (variables defined at the top level of the script or module).
- 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
print
,len
,min
, 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.
Leave a Reply