Table of contents
1.
Introduction
2.
What is Scope in Python? 
3.
LEGB Rule in Python
3.1.
1. Local scope
3.2.
Python
3.3.
2. Enclosing scope
3.4.
Python
3.5.
3. Global scope
3.6.
Python
3.7.
4. Built-in scope
3.8.
Python
4.
Frequently Asked Questions
4.1.
What happens if a variable is defined in multiple scopes?
4.2.
Can a local variable have the same name as a global variable?
4.3.
How can I modify a global variable inside a function?
5.
Conclusion
Last Updated: Aug 16, 2024
Easy

Legb Rule in Python

Author Ravi Khorwal
0 upvote

Introduction

In Python, when we use variables in a program, the interpreter follows a specific set of rules to determine which value to assign to a variable. This set of rules is called the LEGB rule. It stands for Local, Enclosing, Global, & Built-in, which are the different scopes that Python looks at to find a variable's value. The LEGB rule is important for writing clean & effective Python code. 

Legb Rule in Python

In this article, we'll learn what the LEGB rule is, how it works, & see some examples to understand this properly.

What is Scope in Python? 

In Python, scope refers to the region of the code where a variable is defined and can be accessed. It determines the visibility and lifetime of a variable. In other words, scope tells us where in the program a variable can be used.

Think of scope as a container or a boundary that holds variables. There are different levels of scope in Python, which is where the LEGB rule comes into play.

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

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

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

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

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.

Live masterclass