Types of Variable Scope in Python: LEGB Rule in Python

Scope refers to the visibility of variables, parameters, and functions in one part of a program to another part of the same program. In other words, which parts of your program can see or use it? Normally, every variable defined in a program has global scope. Once defined, every part of your program can access that variable. But it is a good practice to limit a variable’s scope to a single definition. This way, changes inside the function can’t affect the variable on the outside of the function in unexpected ways.

LEGB Rule

The scope also defines the order in which variables have to be mapped to the object in order to obtain the value. Let us take a simple example as shown below:

  1. x:= ‘outer x variable’
  2. display():
  3. x:= ‘inner x variable’
  4. print x
  5. display()

When the above statements have executed the statement (4) and (5) display the result as

Output:-
outer x variable
inner x variable

Types of Variable Scope in Python: LEGB Rule in Python

The above statements give different outputs because the same variable name x resides in different scopes, one inside the
function display() and the other in the upper level. The value ‘outer x-variable’ is printed when x is referenced outside the function definition. Whereas when display() gets executed, ‘inner x variable’ is printed which is the x value inside the function definition. From the above example, we can guess that there is a rule followed, in order to decide from which scope a variable has to be picked. The LEGB rule is used to decide the order in which the scopes are to be searched for scope resolution. The scopes are listed below in terms of hierarchy (highest to lowest).

  • Local(L) – Defined inside function/class
  • Enclosed(E) – Defined inside enclosing functions (Nested function concept)
  • Global(G) – Defined at the uppermost level
  • Built-in (B) – Reserved names in built-in functions (modules)
LEGB Rule Diagram

Types of Varibale Scopes

Types of Variable Scope: There are 4 types of Variable Scope, let’s discuss them one by one:

Local Scope

Local scope refers to variables defined in the current function. Always, a function will first look up a variable name in its local scope. Only if it does not find it there, the outer scopes checked. Look at this example

Local Scope Example

On execution of the above code, the variable a displays the value 7 because it is defined and available in the local scope.

Global Scope

A variable that is declared outside of all the functions in a program is known as a global variable. This means the global variable can be accessed inside or outside of all the functions in a program. Consider the following example

On execution of the above code the variable a which is defined inside the function displays the value 7 for the function call Disp() and then it displays 10 because a is defined in the global scope.

Global Scope Example

Enclosed Scope

All programming languages permit functions to be nested. A function (method) within another function is called a nested function. A variable that is declared inside a function that contains another function definition within it, the inner function can also access the variable of the outer function. This scope is called the enclosed scope. When a compiler or interpreter searches for a variable in a program, it first searches Local, and then searches Enclosing scopes. Consider the following example

Enclosed Scope Example

In the above example, Disp1() is defined within Disp(). The variable ‘a’ defined in Disp()
can be even used by Disp1() because it is also a member of Disp().

Built-in Scope

Finally, we discuss the widest scope. The built-in scope has all the names that are pre-loaded into the program scope when we start the compiler or interpreter. Any variable or module which is defined in the library functions of a programming language has Built-in or module scope. They are loaded as soon as the library files are imported into the program.

Built-in Scope Example

Normally only Functions or modules come along with the software, as packages. Therefore they will come under the Built in scope.

Leave a Comment

Your email address will not be published. Required fields are marked *