Types of Scope of variables
There are four types of the scope of a variable.
Source: Link
Local scope of the variable
Local Variables are those variables that are restricted to a single block in which they were created. They are narrowed to only within the function or class and cannot be accessed outside them.
Sample code
def fun():
s = "I'm a proud ninja!"
print(s)
fun()
print("\n",s)
Output:
As you can see, the variable s was created inside the function; therefore, the function was able to print it. But outside the function block, the variable s was not defined. The value of variable s is restricted in the function block only, and it cannot be accessed outside of the league.
Must Read, Divmod in Python
Global Scope of variable
As the name suggests, global variables are those variables that any program block can access. There is no restriction in the global variable and can be used either inside or outside a block. Generally, they are created at the beginning of the program.
Sample Code
s = "I'm a proud ninja!"
def fun():
print("Inside function block: ",s)
fun()
print("Outside block: ",s)
Output:
We can create a global variable by mentioning the “global” keyword before the variable name. For example,
Sample code
def fun():
global s
s = "I'm a proud ninja!"
print("Inside function block: ",s)
fun()
print("Outside block: ",s)
Output:
Here you can see that the string was accessed outside the function with the help of the global keyword.
Recommended Article: Python Square Root
Enclosing Scope of variable
Enclosing scope is only applicable in Python. It is a remarkable scope that is applicable in nested functions. Let us understand this with the help of an example.
Sample code
def outerfun(num):
number=num
def innerFun():
print(number)
innerFun()
outerfun(500)
Output:
In this example, the innerun() function is called only when the outerfun() is called. Now, when we add some integer in the variable, the output will be
Sample code
def outerFun(num):
number=num
def innerFun():
number = number + 45
print(number)
innerFun()
outerFun(500)
Output:
It gave an error. Now, this is when nonlocal variables come into the picture. Nonlocal variables are used in nested functions, which are not the local variable of the inner function. It is created inside the internal function by using the nonlocal keyword.
Sample code
def outerfun(num):
number=num
def innerfun():
nonlocal number
number = number + 45
print(number)
innerfun()
outerfun(500)
Output:
You can practice by yourself with the help of online python compiler.
Built-in Scope of Variable
The built-in scope is used when the variable is not found in local or global or enclosing Scope; then compiler starters look in the built-in directory. It is an enormous Scope that covers all the keywords, and it is effortless to call them without the need to define them.
Also Read, leap year program in python
FAQs
-
What is the Scope of the variable?
The Scope of a variable is its existence in the program,i.e., the Scope of a variable is the block of code in the entire program where the variable is declared, used, and can be modified.
-
What do you mean by the visibility of a variable in Python?
The concept of the Scope of variable rules how variables are looked up in the program that decides the visibility of a variable within the code.
-
How many types of Scope of variables are there in Python?
There are four types of the Scope of variable: Global, Local, Enclosing, Built-in scopes.
-
How do you declare a variable in Python?
There is no need to report a variable in Python; it is a statical language. The variable is created once it is assigned a value.
Key Takeaways
In this article, we have extensively discussed the Scope of variables, their types, and their implementation in Python. We hope that this blog has helped you enhance your knowledge regarding the scope of variables and if you would like to learn more, check out our articles here. Do upvote our blog to help other ninjas grow. Happy Coding!