The LEGB Rule Explaining Scope in Python Programming Language
Sep 06, 2024 3 Min Read 1771 Views
(Last Updated)
Why understand Scope in Python?
Readability or ease of writing doesn’t make a programming language ideal. There are hundreds of elements that come together and frame an ideal programming language. Among those hundreds of elements, the Scope is one of them.
In this write-up, I’ll give you a complete description of Scope in Python. However, Python is one of the easiest programming languages available right now, though it is not perfect. There are a few concepts of Python that could drive you crazy sometimes.
If you are a programmer, the concept of Scope in Python might seem easy to you. However, if you are a beginner in Python programming, Scope in Python can be a nightmare for you!
Since you are reading this, you don’t have to worry, as, by the end of this guide, you won’t have a single doubt regarding Scopes in Python. In this guide, I’ll focus on the following topics:
- Definition of Scope
- Resolution of Scopes
Table of contents
- Definition of Scope in Python
- Basic Scope Representation
- Resolution of Scope in Python
- Local Scope
- Enclosed Scope in Python
- Global Scope in Python
- Built-in Scope
- Final Notes
- MCQs related to the Scope in Python
Definition of Scope in Python
Technically, Scope is a part of a program where an element (variable, function, constant, or object) with a name can be recognized.
For instance, if a variable “x” (hypothetical) is accessible in 5 code statements, then the area of those five statements will be considered as the Scope of the variable “x.” Want to learn how to create variables in Python then click here.
Refer to the variable with its name “x” beyond those five statements. You might not be able to access it because the binding of that name “x” with the variable is only limited to the region of 5 code statements.
Before diving into the next section, ensure you’re solid on Python essentials from basics to advanced level. If you are looking for a detailed Python career program, you can join GUVI’s Python Career Program with placement assistance. You will be able to master the Multiple Exceptions, classes, OOPS concepts, dictionary, and many more, and build real-life projects.
Also, if you would like to explore Python through a Self-paced course, try GUVI’s Python Self-Paced course.
So, this is what the term scope represents in a programming language. If you are still confused, then refer to the program below:
Basic Scope Representation
Output:
If you are getting confused about the working of the above program, don’t worry, the next section is entirely about scopes and their resolution.
Resolution of Scope in Python
Different programming languages have different rules for resolving the Scope. Similarly, Python has a LEGB rule. Every character of the term “LEGB” has a specific meaning and represents a particular scope type.
Note: The scopes mentioned below are arranged in the Narrowest to Broadest range.
- Local: This Scope is valid for all the defined elements inside a function or a class.
- Enclosed: Names defined in nested functions or closed functions.
- Global: Names defined above a Python class or at the top of a module
- Built-in: All the predefined names in Python directories.
If you are confused with the definition, don’t panic! I will explain each one of them in detail in the next section. But before that, have a look at the image depicting the above terms.
So, I have briefed you about the scope resolution rule, i.e., LEGB. Now, let’s have a look at the detailed explanation of name scope from the LEGB Rule.
Local Scope
This section is about the first name scope of the LEGB rule, i.e., Local Scope.
Here in the above example, In the ‘display()’ function, the ‘var’ variable, defined in the method itself, is invoked. According to the LEGB rule, the method must look for the Local Scope in the first place, and that’s what is happening in the above display method.
Output:
If the method can’t locate the name in its local Scope, it will look into the enclosed Scope. A detailed illustration is as follows:
Enclosed Scope in Python
In this illustration, the method ‘printvar()’ is trying to print the variable ‘var.’ As per the scope resolution rule, method ‘printvar()’ should look into its local Scope to find the variable ‘var.’
In this case, no variable is defined in the local Scope; therefore, the method ‘printvar()’ has to look into the enclosed Scope to find the variable that’s how the variable ‘var’ was located by the ‘printvar()’ method.
Output:
Global Scope in Python
In the above illustration, no variable is defined in method ‘display()’ or nested method ‘printvar()’; therefore, there is no local or enclosed scope variable; thus, method ‘printvar()’ will look for the global scope variable.
Output:
Built-in Scope
If a method can’t locate any global, enclosed, or local variable, it automatically looks for built-in Scope. For instance,
Here, if you look closely, you’ll find that the method ‘printvar()’ is printing the variable ‘e,’ but this variable is neither defined in any function nor at the top of the file.
Therefore, the method couldn’t find the ‘e’ variable in local/enclosed or global Scope; as the last option, the method will look for the built-in scope names.
Here the ‘e’ is imported from the ‘math’ module and acts as a built-in scope variable.
Output:
Kickstart your Programming journey by enrolling in GUVI’s Python Career Program where you will master technologies like multiple exceptions, classes, OOPS concepts, dictionaries, and many more, and build real-life projects.
Alternatively, if you would like to explore Python through a Self-Paced course, try GUVI’s Python Self Paced course.
Final Notes
In this guide, you have learned about different scope variables, and LEGB rules for scope resolution. Besides this, you have also learned about “How methods locate different variables based on their scopes.”
Now, all you have to do is “Practise it thoroughly.” Looking at the program won’t be as effective as writing them on your own. It’s okay if you run into errors, you can refer to different guides available on the internet for debugging your program.
You may practice Python coding on our award-winning Codekata platform.
CODEKATA- GUVI's Practice Platform
MCQs related to the Scope in Python
- Which of the following definitions define global scope?
- Names that are declared at the top of the program, outside the functions.
- Every function present in the program can access the Name.
- Both a & b
- None of the above.
Answer:(c)
- The Names declared in the local scope can be accessed within the current function only.
- True
- False
Answer:(a)
- What will be the output of the following program?
- 20
- 22
- Run-Time Error
- None of these
Answer:(b)
- If in a variable, a local variable declared in function fx() and a global variable have identical names then which variable will the function fx() invoke?
- Local Variable
- Global Variable
- Run-time Error
- A null value will be printed
Answer:(a)
- Which thumb rule helps to resolve the scopes in Python
- LEGB
- LGEB
- BGEL
- None of the above
Answer:(a)
Did you enjoy this article?