Javatpoint Logo
Javatpoint Logo

Python Closure

A language like Python has abundant concepts that are interesting and are meant to ease the work of programmers.

In this tutorial, we will learn about python closures. But before that let us revise nested functions and see how they can act as a pre-requisite to understand this topic.

So, let's get started:

Program 1:

The program given below illustrates the concept of nested functions and non local variables.

Output:

Python at JavaTpoint

Explanation:

Let us understand what we have done in this program-

  1. We have created function_1 that takes the parameter txt and inside this, we have made another function that prints the value of txt.
  2. As we can see 'function_2' is called in the above program and then we have passed a string value in function_1.
  3. On executing this program, it displays the desired output.

Program 2:

Now, let's see how closure can help in making our work simpler and enhancing our program.

The following program illustrates the same-

Output:

Let us learn a programming language.

Explanation:

Let us understand what we have done in this program-

  1. We have created function_1 that takes the parameter txt and inside this, we have made another function that prints the value of txt.
  2. As we can see the value of 'function_2' is returned in the above program and then we have passed a string value in function_1 and allotted that to function_3.
  3. On executing this program, it displays the desired output.

Observation:

Based on the above observation, we can conclude that-

  1. With the help of closure, we can invoke functions that are outside the scope in Python.
  2. We can say that closure is a function object that remembers the values present in the enclosed scope.
  3. It is a record in which each variable of a function is mapped with the value or a reference to the name when the closure was created.
  4. It acts as an aid to fetch or access the variables with the help of closure copies.

When We can use a Closure

We can use the closure in the following conditions:

  1. A program must have a nested function.
  2. The function should refer to a value in the enclosed function.
  3. The enclosing function should return the nested function.

Some More Features-

Features of the Closures are:

  1. Closures provide a kind of data hiding in our program and so we can avoid using global variables.
  2. It is an efficient option when we don't have too many functions in our program.

Program 3:

Finally, let's have a look at one more interesting program that uses closure.

The program shows the situation when we can use a closure instead of class.

Output:

6
10
17

Explanation:

Let us understand what we have done in the above program-

  1. The function that we created is add_num and inside this, there is another function called addition.
  2. This function is supposed to return the x+n value.
  3. In the next set of statements, we have used different ways of adding two numbers.
  4. On executing the program, it displays the required result.

Conclusion

So, in this tutorial, we learned what are closures and when we can use them to make our program more effective.


Next TopicPython Glob Module





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA