hey guys welcome to the next blog on Python tutorial for beginners in this blog I'm going to show you how to use nested functions and closures in Python so let's get started so we will start with the nested functions so in Python we can define the nested function that means we can define a function inside a function so let me give you this example and in this example this function which is an outer function takes some text or message as the argument and I have defined one more function inside the outer function and I named it as inner function which prints the value of the text which we have passed in the outer function and we are just calling the inner function in the outer function scope so this function is declared locally inside the outer function and we are just calling this inner function inside this outer function now in other words we can also say that this outer function is an enclosing function and this inner function is the local function of this outer function and now when I call this outer function and let me run the code it's going to just print the value which we have passed as an argument so we have just passed this text argument which is once again passed to print statement of inner function and because we are calling this inner function inside this enclosing function it prints the value using this statement so when you declare one function inside the another function it's called the nesting of functions let me give you one more example of the nested function and I have defined this pop function which is our outer function or enclosing function and here I have defined a function called get last item which is our inner function or the local function to the pop function now this puffs function takes the list as an argument and we have defined a local function inside this pop function and what this local function get last item does is whenever you pass a list as an argument to this local function it's going to find out the last element of the list and return the last value of this list so here this is the last index of the list and then it's going to return the last item of the list which is passed as an argument now you may already know that you can call a function called remove on your lists which is used to remove some item from the list so I'm just calling remove function on the list and as an argument of the list I'm passing the function which finds out the last element of the list so this is going to give us the last element of the list and this last element will be removed from the list and at last I'm simply returning the list which is passed as an argument to the pop function so let's say this is the list on which we are working on which have five elements and we are calling pop function on this list again and again three times so let me run the code and let's see what happens so you can see when first pop is called it's going to remove the last element which is 6 from the list and then whenever the second pop is called it's going to remove the 4 and whenever the third pop method is called it's going to remove the 3 and so on so this is how you can use nested functions in Python now let's talk about the closures so I'm going to use the same nested function example which I have shown you earlier so this is the nested function now in order to convert this nested function into a closure what we need to do here is we need to return the inner function without the parentheses so the thing to note here is we don't need to return this inner function with the parentheses we need to turn this inner function without any parentheses and this is the simplest example of the closure so what is a closure so a closure is a function whose return value depends on the value of one or more variable which are declared outside the function so in this example this text variable is declared outside the inner function and the value of this inner function depends on this text variable which is declared outside this inner function and that makes it a closure and closure have a special property that this closure function object remembers the value in the enclosing scope even if they are not present in the memory so I will explain you that but let me just show you how to use this outer function which now uses that closure so I'm going to declare a variable a which means because this outer function is returning the inner function that means a contains now the inner function so we can use this a as the function so in order to use this a as the function we can just call this a using the parentheses because this function doesn't take any arguments so we are not passing any argument here but we will just call it as a function because this outer function is just returning the inner function so let's run the code and let's see what happens when we run this code so you can see it prints hello which is printed using this print statement whenever we call this a function so if I just call this a function without the print method also it's going to print the hollow because here we are just using the print to print the text which is passed using the outer function now as I said closure is a function object that remembers the value in the enclosing scope even if they are not present in the memory so our enclosing scope is the outer function so even if we delete the outer function after declaring the statement and if the a contains this inner function and now let's say I'm going to just delete the outer function so this statement is going to delete the outer function and let me call this outer function once again after the deletion of this function that means it's going to throw us an error which will say that this function is already deleted so we cannot call at this function and now let's see the magic of the closure so even if we deleted the outer function and we cannot call the outer function but before deleting we have created a variable which contains the value of inner function and now I'm just calling the inner function with these parentheses and let's see what happens so I'm going to just run this code and it's going to print hello even after we have deleted the outer function so this means that this variable a is storing some kind of state of inner function even if the outer function is deleted and that is the magic of the closures so a closure function is able to remember the values which are declared outside the function also so this is our closure function and it's able to remember the value which are declared which is text outside the function and that's the beauty of closures now let me give you one more example of the closure so we will be able to understand it in a better way so here I have defined a function called nth power and I pass one argument here which is an exponent and inside this nth power function I have defined a local function which also takes one argument and then what it returns is is the power of whatever argument we pass here and this exponent is coming from the outer scope which is as an argument of the outer function which is aunt power so base is coming from the inner local function and the exponent is coming from the outer scope and we are just returning this power of which is a local function without any parentheses once again so this is very important you return the function without the parentheses now I can declare some variable first of all I'm going to declare a variable called square and then we will call this nth power function and we will just pass the value to so now this exponent becomes 2 here which is also passed to the inner function or the local function so here the value of 2 is saved that means whenever we call the square function it's going to give us the square of whatever number we are going to pass as an argument to this square so let's try eight months and let's see what happens so I'm going to just print and then call this square function which takes an argument and I'm going to first of all find out the square of two and I'm going to run this program and you can see it's going to give us the square of this two because now the square function is going to give us the square of the number which we will pass as an argument here so this 2 is passed to the inner function because this nth power gives us the inner function because it returns the inner function so now this to which we are at passing in the square is passed as the base of this in our function and that's why we are getting the square of the number which we pass here so let's find out the square of some more numbers so I'm going to just pass 3 4 and 5 here and I'm going to run the code once again and you will see that it's going to give us the square of 2 3 4 and 5 so once again we have seen that the closure function is remembering the value which is declared outside the scope which is exponent now whenever I use this function once again so let me declare one more variable and this time I want to declare a cube here and once again I'm going to use the nth power to find out the cube of numbers so now this 3 is passed as an exponent here and this exponent is going to be passed inside the inner function so exponent value is 3 here that's why we are going to get the cube of the number which we are passing as the base argument of this inner function so now this is the inner function and we can once again call the print to call the cube method this should be cubed not cure so cube and then we can find out the cube of 2 and let's find out the cube of 3 4 and 5 also so let's run the code once again and now you will see here the cube of the numbers are printed whatever numbers we are passing as an argument of this cube function so in a way this cube or the square variable is holding the status of the inner function and that's something we have also seen in the case of classes the classes are able to remember the state of the variables and the methods which are declared inside that losses so closures are sometimes used in place of the classes which only have usually one method inside them because this one method we can already define in the closure also and it's able to remember the state so closures can be used in place of the classes which have fewer method generally one method inside them the closures are also used heavily in the case of decorators in Python so decorators we are going to learn in the next blog and I'm going to show you why decorator use closures and how to use closures with that decorators and the third advantage of closures are they are sometimes more efficient than the normal functions so the closures are sometime also used for the code efficiency and the faster working of code so this is how you can use closures in Python I hope you've enjoyed this blog and I will see you in the next blog
Post a Comment