The Types of Variable Scope in Python. Explained!

Types Of Python Variable



The Types of Variable Scope in Python. Explained!

Introduction:

There are times when you need to learn things based on your interests and skillsets. But you also need to be careful enough about how you’re learning them and working on them simultaneously. Python is undoubtedly the most important and the most in-demand programming language that is thriving across the whole world. You won’t see a single developer who doesn’t work or discuss Python Programming. 

When you are working on the Python variables it is a bit difficult to understand whether you’re familiar with these variables or not. 

In this blog, you will get to know the different scopes of variables as well as the LEGB rule (Local -> Enclosing -> Global -> Built-in) that is used while the Python interpreter works with the variables.



Types of variable scope in Python: 

Local Scope


The moment you define a variable within its function, its scope lies only within the function body, thus it can’t reach outside the function body. This is the nature of the Local scope. The variable is created at a function call, not in the function definition. Therefore we can have as many different local scopes as function calls. This is relevant even if you call the same function numerous times. A new local scope will be created with each call.

Here’s an example for your better understanding. 

Example:

Code:

num=0

Def demo ():

#print (num)

num=1

print(“The Number is:”,num)

demo()

Output:

The number is:1

Thus Num is declared local to the function. If we use the num variable in the function before declaring it locally, it will raise an error. 


Global Scope

Global or module scope is the utmost scope in a Python program, module or script. It is the easiest scope to understand. This scope consists of all the variables that you define at the top level of a module or program. Variables in this scope are visible everywhere in your code and can be accessed from anywhere in your code. When we define a variable outside any function, it becomes a global variable and its scope is present within the program. The global variable value can also be changed. This means it can be used by any function.

Here’s an example for your better understanding. 

Example:

Code:

Def demo():

Print (Str)

#Global

Str= “You are clever”

demo()

Output:

You are clever



Enclosing Scope

Enclosing or nonlocal variable scope is a specific scope that exists only for nested functions. To create a nonlocal variable it's required to use a nonlocal keyboard. If we consider the local scope to be an inner or nested function, then the enclosing scope is the scope of the outer or enclosing function. This scope contains the name that is defined in the enclosing function. The variables in the enclosing scope are visible from the code of the inner and enclosing functions. This means the variable is neither in the local scope nor in the global scope. Scopes that aren't global or local come under enclosing scope.

Here’s an example for your better understanding. 

Example:

Code:

def func_outer():

x= “local”

def func_inner():

nonlocal x

x= “nonlocal”

print(“inner:”,x)

func_inner()

print(“outer:”,x)

func_outer()


Output:

inner: nonlocal

outer: nonlocal


If we just use the outer function’s value and do not want to do any changes, then comment (nonlocal a) line.


Built-in Scope

The built-in scope is the widest scope that exists and is a special Python scope that is created or loaded when we run a script or open a session that is interactive. The variable which is not defined in the local, global or enclosing scope, is found in the built-in scope. Therefore, the variable present in the built-in scope shouldn’t be used as an identifier. This scope consists of names such as functions, keywords, exceptions and other attributes. In your code, names are also available everywhere in the Python scope. When you run a program or a script they load automatically by Python. This scope contains all the special reserved keywords. In your program, you can call the keywords anywhere without defining them before use. These keywords cannot be used for any other purpose but only for specific purposes.

Here’s an example for your better understanding. 

Example:

Code:

#Built-in Scope

from math import pi

#pi= ‘Not defined in global pi’

def func_outer():

#pi= ‘Not defined in outer pi’

def inner():

#pi= ‘not defined in inner pi’

print (pi)

inner()

func_outer()

Output:

3.141592653589793




Conclusion

Now we have an idea about the types of variable scopes as well as the LEGB rule. Now without any problem, you can easily manipulate variables in nested functions. Hope this article was helpful for you.


Comments

Post a Comment

Popular posts from this blog

4 Ways To Unlock Your Potential As Fresher To Get A Job In Python

What are the roles and responsibilities of a Python Developer?