Python Tutorial for Beginners 51 - Global, Local and Nonlocal variables in Python

 


hey guys welcome to the next blog on Python tutorial for beginners in this blog we will talk about global variables local variables and non local variables so let's get started so to start with I have a very simple function which is funk and here it has one print statement which is printing the value of x but you can see here X is not defined locally inside the function instead I have declared X outside the function and I'm just assigning the value to X just before calling the function here so what do you think will happen so let's run the code and let's see what happens so because this X is declared globally and it's in the global scope so first of all your function will search for the value of x inside the function in the local scope and if it doesn't find the value x inside the local scope it's going to see outside the local scope in the global scope for the value of x and we have defined this value of x outside the function in the global scope so this print statement will take the value of x from the global scope and that's why it's printing global using this print statement now let's modify this function little bit so I'm going to define a local x inside the function and I'm going to assign a new string to it so let's say this is a local value so I am going to assign the string to the X so now we have X variable inside the local scope of the function and also we have another X variable outside the function in the global scope so what do you think now will happen so let's run the code and let's see what happens so you can see here it's going to print local because the variable will take the local value or local value have more priority than the global value so whatever variable declared nearer to the use of this variable that variable value will be used and that's why the local is printed because it's declared nearer to the use of this X which is inside the local scope of this function now let's print the value of x outside the function so in the global scope I want to print the value of x once again and let's run the code and let's see what happens so this local is printed using this print statement and the global is printed using the print statement which is outside the function so right now things are easy to understand when you declare this value x outside a function in the global scope and call the value of x using print then it's going to print that global and whenever you use the variable inside the function in the local scope it's going to print the local variable value which is local to the function so this means variable X which you have declared inside the function is the local variable and this variable x which you have declared outside the function is the global variable now let's make things a little bit interesting and I want to just print the value of x before even assigning the value local to it so right now I want to do something like this so until this print statement I haven't declared the value of x locally so using this print statement the value of x is supposed to be taken from the global variable which is this variable and then once we have declared the value of x locally then this print statement is going to print the value of x using the local variable but when we run the code it's going to give us the error and it's going to say that this is unbound local error which is local variable X referenced for the assignment so right now what we are trying to do is we are trying to mix the global and the local variables and Python is not able to understand whether X is a local variable or X is a global variable so to solve this problem we need to state explicitly that this X which we want to call using this print statement is the global value right so X here we want to take from the global value so to state that we can use a keyword called global and then the variable name so by writing this kind of expression we are telling Python that X is declared globally outside the function and we want to take the value of x globally so using this statement we are just stating that use the value of x using that global scope now when we run the code it's going to run fine so now you can see it has printed global here so let me just change this print statement so we will be able to understand what's happening really here so I'm going to just assign some number to the print statement let's say this is number one print statement and then this is number two print statement and this is number three print statement so we are going to know which value is printed using which print statement so I'm going to run the code once again and now you can see first print statement which is this one is going to print global because until here the value of x is taken from the global scope so it's going to print the value of x which is global which is defined here now you can see here we are reassigning the value of x and we are just saying that now we want to assign local to this X variable but still this variable is the global variable because we have explicit we stated here that take the value of x from the global variable so this global variable value will be reassigned and now this global variable value is local because we have reassigned the new value inside the global variable X and in the third print statement because we have already changed the value of x here now the new value of x will be local and it will be accessible from the global scope also so this also prints local so once again when you use this expression you are telling your local function that used the value of x from the global scope so now whenever you reassign some other value to X this is not the local declaration you are just reassigning the value to the global variable which is X here okay so once you have changed the value of x to local it will stay there that's why here also it has printed the value of x is equal to local now let's talk about the non local variables so non local variables behave almost similar to global variables but they have some differences so let's say I will define instead of saying that this is a global variable I'm going to say that this is a non local variable and when I run the code it's going to complain and this is going to say that this is a syntax error and no binding for non local variable is found so non local variable bindings you can only do inside the nested function so generally you use non local variables inside the nested function you cannot use this non-local keyword instead of the global keyword so let's see how we can use the non-local variables inside the nested function so what I'm going to do here is let's say I'm going to define the value and let's say this time I want to just use the value of x as numbers so here I'm going to assign X is equal to 20 so this X is in the global scope and I'm assigning 20 to it and then I'm going to once again inside the function I'm going to declare a local X and I'm going to assign 50 to it and then I'm going to define one more function inside this function and I'm going to name it as let's say in a function and let me just add the d EF keyword and then inside this inner function what I'm going to do is I'm going to define a non local variable once again X and then what I'm going to do is I'm going to reassign the value of X is equal to hundred so what I have done I have declared this X global variable and assign 20 to it and inside the function also I have declared one local X variable and I have assigned 50 to it and inside the inner function of this function I have defined a non local variable which is X and I have assigned the value hundred to it so now what we are going to do here is we are going to call this inner function inside our outer function which is the funk function okay so this is the inner function and we are calling this inner function inside this function which we have declared so now what do you think will happen so let's run the code and let's see what happens so this first print statement which is this one it's going to print 50 because until now before calling the inner function the value of x will be taken from the local scope because we haven't stated here explicitly that x is a global variable so the value of x will be taken from the local scope right so we haven't written here that value of x is a global variable X value right so that's why the value of x will be taken from the scope and that's why it's printing 50 here using the first print statement now here we have called the inner function that means the value of non-local variable is assigned 200 and once again we are calling the value of X so here inside the inner function we have defined the non-local variable that means it's not a global variable so it's not changing the value of the global variable or it's not using X from the global scope but this X will be taken from the local scope so this X will be overwritten by hundred values this is the new value we are assigning to our local variable which is X right so that's why it's just giving us hundred using this print statement so this non-local keyword you generally define inside the nested functions to state that we want to take the value of x from the local scope of the outer function not the global scope so this X is essentially this X which is defined inside this function and we are just reassigning the value of x to hundred and that's why it's printing hundred here and also you can see this print statement which is printing the value of global X which is this one that's why it's printing 20 so because we are not using this global X inside the functions because we have just used the local variable X inside the function and then we have reassigned the value of 100 to this local variable this X is not going to change and that's why it's printing 20 here now let's see what will happen when we change this keyword non-local to global and once again I'm going to run the code so now let's go to the function and here we have defined the local X which is different from the global scope so I until here the value of x will be taken from the local scope so this print statement because we haven't called the inner function before this so the value of x will be taken from the local scope so here it's going to print 50 now after this print statement we are just calling the inner function and inside this inner function we are just saying that take the value of x from the global scope not the local scope so using this expression we are saying that take the value of x from the global scope so this value will not be affected by this reassignment because now we are changing the value of the global X and not the local X so that's why when you call a dis print statement the value of x will be taken from the local scope so using this print statement when you call X it's going to take the local value of X because this inner function has no effect on this value of the local X and that's why it's printing 50 once again using this print statements so this x value will come from the local X and not from the global X and the last print statement is going to print hundred because this is the global X we are talking about and we have already changed the value of global X 200 using this expression that's why it's printing hundred here so there is a difference between non-local and the global variables if you write here non-local then here this value will be treated as the local variable so here we are changing the value of the local variable of X if you declare this as the global variable then we are changing this value which is in the global scope so this is how you can use global variables local variables and non-local vary in Python I hope you have enjoyed this blog I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News