In this Python basics tutorial we will study about local, global and non local variable of Python with examples.
Local Variable
Local variables are those variable which are local to some function or block. These variables are accessed only the scope of function inside which they are created.
Example of local variable
Let’s understand local variable in Python by taking example
#Python local variable example def f1(): x=12 print(x)
Output
NameError: name 'x' is not defined
Explanation:
Here x is defined inside function f1. When we try to access x outside function it throws error that x is not defined. Because x is local variable and can be accessed only the function or block inside where it is created.
Global variable
Global variables are those variable which are declare outside any function or methods and can be accessed anywhere in the program, In Python also, we can have global variable.
Characteristics of Global Variables
- Global variables are declared outside any function or block.
- In Python, there is no need to explicitly write global against variable name.
- Global variables can be accessed anywhere in the program.
Example of global
#Python global variable example x=2 def f1() : print(x) f1() print(x)
Output
2
2
Non local Variable
Python has very interesting variables called non local variables. Non variable are used in nested functions in Python. Nested function are functions inside function. Non local variable act like the global variable.
As we study that any variable inside function is local to that function means we cannot access that variable outside the function.
In nested function, one variable of outer function can be global to inner function, this can be achieved by nonlocal keyword.
Example of non local variable
Let’s understand this by taking one example:
x=12 def f1(): x=10 def f2(): nonlocal x x=x+1 print("value of x inside f2 function",x) f2() print("value of x inside f1 function",x) f1() print("value of global x is",x)
Output
value of x inside f2 function 11
value of x inside f1 function 11
value of global x is 12
Explanation
- In above example, x is global variable to program and we have two functions, outer function is f1 and inner function f2.
- Inside f1 function we have declared one variable x, this x variable is local to function f1 and it has different value than global variable x (at line 1)
- Inside inner function f2, we have nonlocal variable x, now this nonlocal variable x is bind to variable x defined in function f1, so inside f2 function, variable x will act like global variable. Any alternation or change in value to x inside function f2 will change the value of x of function f1.
That’s why when we did x=x+1 then it will increment value of x which is 10+1=11.
- Now if we print the value of x inside function f1, it will also be 11 not 10. It shows that local variable x of function f1 acts as global variable to function f2 by using nonlocal keyword.
- At last, global variable x at line 1, is of value 12. And we print it at last.
Nonlocal variables are always bind with local variable to make then non local as name suggested.