Closures in Python with Examples

Advertisement

In the previous blog, we learned about Inner Functions or Nested Functions in Python. This time we are going to extend the previous blog to discuss a whole new topic i.e Closures in Python. We will also have so many examples to describe closure in python.

Let’s learn about Closures in Python.

Before learning about closures in python make sure you have a good knowledge of Python Inner Function or Nested Function, as those examples will be extended in this topic.

As discussed earlier, Nested functions are functions inside a function (also a feature of Encapsulation) in python. The inner or nested functions have the ability to access the variables of the outer function and can also modify it if used as a non-local variable. Check out the previous blog if you didn’t understand anything.

Python Closures

python closures
Image Source – towardsdatascience.com

Then what are closures? Actually, it’s very simple, A closure is a function object that has the access to the outer function variable even if the outer function has returned.

Let’s take an example to learn closure.

def f1():
    name = "HTD"

    def f2():
        return f"Hello, {name}"

    return f2


func = f1()
print(func())
Hello, HTD

Let’s breakdown the example in order to understand it properly.

f1() is the outer function that has a 'name' variable and an inner function f2(). The Function f2() uses the outer function’s name variable to return a string value. The f1() function, at last, returns the inner function.

Here is a catch, Earlier the outer function used to return the value returned by the inner function, but here we are not calling the inner function and directly returning the inner function.

At the global scope, the func variable stores the f1() function call which returns the f2() i.e (the inner function of f2()).

The func variable as it stores a function is executed and it gives us the result.

One thing to notice here is that the name variable has a lifetime only till the function f1() is in memory, when the f1() is returned the name variable is also removed from memory, then how the inner function is able to use it.

This happens because the python uses closures feature, which allows the inner function to store the variable of the outer function and thus can use it.

Criteria for closures

There are three important points that should be fulfilled in order to work with closures in python.

  • There should be nested functions.
  • The inner function must use the variable of the outer function.
  • The outer function should return the inner function.

If these three conditions are met, the python will use its closure feature. In the above example, all three conditions are met and hence we can see python closures work.

Hope you like it.

Learn more about python like list comprehension, store web scraping to CSV using python.

About Author
5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Scroll to Top