LEGB Rule in Python
The LEGB rule is a way for Python to find the value of a variable. When you use a variable in your code, Python will look for its value in a specific order:
1. Local scope
First, Python looks for the variable in the local scope. This means it checks inside the current function for a value assigned to that variable name.
For example :
Python
def my_function():
x = 10 # Local variable
print(x)
my_function()
print(x)

You can also try this code with Online Python Compiler
Run Code
Output
10
Error: x is not defined
In this example, x is a local variable defined inside my_function(). It can be accessed within the function but not outside of it.
2. Enclosing scope
If the variable is not found in the local scope, Python moves on to the enclosing scope. This is relevant when you have a function inside another function (nested functions). Python will check the outer function for the variable's value.
For example :
Python
def outer_function():
x = 10 # Enclosing variable
def inner_function():
print(x) # Accessing enclosing variable
inner_function()
outer_function()

You can also try this code with Online Python Compiler
Run Code
Output:
10
In this code, x is defined in the enclosing scope of outer_function(). The inner_function() can access x because it is in its enclosing scope.
3. Global scope
If the variable is not in the local or enclosing scope, Python looks in the global scope next. The global scope contains variables that are defined outside of any function & can be accessed from anywhere in your code.
For example :
Python
x = 10 # Global variable
def my_function():
print(x) # Accessing global variable
my_function()
print(x)

You can also try this code with Online Python Compiler
Run Code
Output:
10
10
In this code, x is a global variable defined outside any function. It can be accessed both inside my_function() and outside of it.
4. Built-in scope
Finally, if the variable is not found in any of the above scopes, Python will look in the built-in scope. This scope has all the built-in functions & variables that come with Python, like print(), len(), etc.
For example :
Python
print(len("Hello"))

You can also try this code with Online Python Compiler
Run Code
Output
5
Here, len() is a built-in function that can be used directly without any import statement.
Note: Python follows this order to ensure that you're using the right value for a variable based on where it's defined in your code.
Frequently Asked Questions
What happens if a variable is defined in multiple scopes?
Python will use the variable from the innermost scope that has a definition for it, following the LEGB rule order.
Can a local variable have the same name as a global variable?
Yes, but the local variable will take precedence inside the function, temporarily hiding the global variable within that scope.
How can I modify a global variable inside a function?
To modify a global variable inside a function, you need to use the global keyword before the variable name to indicate that you want to use the global variable, not create a new local one.
Conclusion
In this article, we learned about the LEGB rule in Python, which determines the order in which Python looks for variables in different scopes. We discussed the concepts of local, enclosing, global, & built-in scopes & saw examples of how they work.
You can also check out our other blogs on Code360.