Scope of a variable
The scope of a variable basically refers to its lifetime within the program. Global variables are declared outside any function or block and can be accessed throughout a program (global scope). Local variables on the other hand are declared within a function or block and
can only be accessed from inside within the function or block (local scope).
Code for scope variable in python 3.9
In the example given below, the value of the variable num prints differently when it is inside the function printNum() as opposed to when it is outside it.
num = 10 def printNum(): num = 5 print("Inside printNum(): ",num) printNum() print("Outside printNum(): ",num) Output: Inside printNum(): 5 Outside printNum(): 10
The above program is an example which stresses on variable scope. The variable num inside the function printNum() is different from the variable which is outside the function. Inside the function, num is a variable local to the function and hence this value will
get printed. The function will always look for the variable within its local scope before it tries to check outside the local scope. Python uses the LEGB rule to decide where to look for any variable (L-local, E-enclosed, G-global, b-builtin).
def myFunction(): myStr = "This is my function()" print(myStr)
The above program will throw a NameError since the variable myStr is not defined. myStr is a local variable to the function myFunction() and hence cannot be accessed outside its scope.
Global keyword in python 3.9
When we need to modify the value of a global variable inside a function within a local scope, we make use of the global keyword. A value within a function is local until and unless we use the global keyword. Inside a function, we do not need to use the global
keyword if we simply want to access the value of a global variable.
Code for global keywords in python 3.9
num1 = 3 num2 = 4 def findProduct(): product = num1 * num2 print(product) findProduct()
In the above code, we simply access the values of the global variables num1 and num2 inside the function findProduct(). We do not need to use the global keyword since we are not actually modifying the value of the global variables.
num = 20 #global variable def doubleNum(): num = num * 2 print(num) doubleNum()
In the above code, the function doubleNum() is attempting to modify the value of the global variable num. If we try to run the program, we will get an UnboundLocalError since the variable num has been referenced but hasn’t been assigned. The variable num
here is implicitly taken to be a local variable. Thus we have to use the global keyword for our program to work correctly.
case of nested functions in python 3.9
A nested function has access to all the variables of the function within which it is nested. Any value assigned by the primary function can be accessed or changed by any function nested at any level within the primary function.
def func1(): num = 10 def func2(): global num num = 20 print("Before calling func2(): ",num) func2() print("After calling func2(): ",num) func1() print(num) Output: Before calling func2(): 10 Before calling func2(): 10 20
In the code written above, func2() is a function nested inside the primary function func1(). The variable num is assigned a value within func1(). However inside the function func2(), we have defined num as global keyword and then assigned the value of num to be
In this article we saw what are local variables and global variables in python 3.9 so about this article you have any query then free to ask me.
Credit: Arya Gupta