InterviewSolution
| 1. |
Explain the concept of global, local and nonlocal variables in Python. |
|
Answer» Depending upon where in the program a certain variable is declared, it is classified as a global, local or nonlocal variable. As the name suggests, any variable accessible to any function is a global variable. Any variable declared before a call to function (or any block of statements) is having global scope. DEF square(): print (X*x) x=10 square() output: 100Here, x is a global variable hence it can be accessed from anywhere, including from square() function. If we modify it in the function, its effect is persistent to global variable. def square(): x=20 print ('in function',x) x=10 square() print ('outside function',x) output: in function 20 outside function 10However, if we try to increment x in the function, it raises UnBoundLocalError def square(): x=x+1 print ('in function',x) x=10 square() print ('outside function',x) output: x=x+1 UnboundLocalError: local variable 'x' referenced before assignmentA local variable is one which is declared inside any block of statements such as a function. Such a variable has only local presence which means, if we try to access outside its scope, an ERROR will be reported. def square(): x=10 x=x**2 print ('in function',x) square() print ('outside function',x) output: print ('outside function',x) NameError: name 'x' is not definedNote that if we use a variable of the same name as that available in the global namespace inside a function, it becomes a local variable as far as the local namespace is concerned. Hence, it will not modify the global variable. def square(): x=5 x=x**2 print ('in function',x) x=100 square() print ('outside function',x) output: in function 25 outside function 100If however, you insist that your function needs to access global and local variables of the same name, you need to refer the former with ‘global’ keyword. def square(): global x x=x**2 print ('global x in function',x) x=10 square() print ('global x outside function',x) output: global x in function 100 global x outside function 100Python's global() function returns a dictionary object of all global variables keys and their respective values. def square(): print ('global x in function',globals()['x']) x=10 square() output: global x in function 10Python 3 introduced nonlocal keyword. It should be used to refer to a variable in the scope of an outer function from an inner function. Python does allow definition of nested functions. This nonlocal variable is useful in this context. In following program function2 is nested inside function1 which is having var1 as its local variable. For function2 to refer to var1 it has to use a nonlocal keyword because it is neither a local nor a global variable. def function1(): var1=20 def function2(): nonlocal var1 var1=50 print (var1) print ('nonlocal var1 before inner function',var1) function2() print ('nonlocal var1 after inner function',var1) var1=10 function1() print ('global var1 after outer function unchanged',var1) output: nonlocal var1 before inner function 20 50 nonlocal var1 after inner function 50 global var1 after outer function unchanged 10 |
|