How does variable scopes work in Python Modules?


The scope of the Python object determines its accessibility. The scope must be specified in order to access the particular variable in the code because it cannot be accessible from anywhere in the program. The term "scope" describes the precise coding region where variables are shown. It is possible to limit the visibility of variables so that only certain people can see them. Scope confirms which variable can be "Seen".

The scope determines the rules that regulate how and where a variable can be searched. The variable is searched either to assign a value or to retrieve one. The namespace provides a unique identification for the variable or procedure. Namespace details the object's name and the place the Python interpreter is seeking to access.

The scope resolution of the Namespaces is examined in accordance with the LEGB rule. E stands for "enclosed," G for "global," B for "built-in," and L is for "local." The LEGB's order is very important. First, the Local, then the Enclosed, then the Global, and finally the Built-in are searched for the variable.

There 4 main scopes in python namely, Built-In, Global, Enclosing, Local.

Local Scope

The variables that are defined in the function are variables with a local scope. The function body contains a definition for these variables.

Example

Let's use an illustration to assist you comprehend this idea. One variable, num, was used in example 1. Num = 0 is not a local variable because it is defined outside of the function. According to our definition, a local variable is one that is declared inside the body of a function. Here, the local variable num=1 is set and printed inside the demo function.

num=0 def demo(): #print(num) num=1 print("The Number is:",num) demo()

Output

The output generated is as follows.

The Number is: 1

Global Range

A global scope variable is one that can be read from anywhere in the program. You can access these variables both inside and outside of the code. We declare a variable as global when we intend to use it across the rest of the program.

Example

The above example shows how we declared a variable called str outside of the function. When the function demo is invoked, the value of the variable str is printed. The global keyword is not required to use a global variable inside of a function.

def demo(): print(Str) # Global Str = "Hello World" demo()

Output

The output generated is as follows.

Hello World

Enclosing or Nonlocal Scope

The variable that is specified in the nested function is known as Nonlocal Variable. It indicates that the variable cannot be both local and global in scope. The nonlocal keyword is used to generate nonlocal variables.

Example

The inner function is nested inside the outer function that we generated in the following code (). Inner() function is defined in the outer() function's scope. Changes made to the nonlocal variable declared in the inner() function are mirrored in the output of the outer function.

def Outer(): x = "local" def Inner(): nonlocal x x = "nonlocal" print("inner:", x) Inner() print("outer:", x) Outer()

Output

The output is as follows.

inner: nonlocal
outer: nonlocal

Updated on: 16-Sep-2022

912 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements