Table of contents
1.
Introduction
2.
Nested functions in Python
2.1.
Python
3.
What are Python Closures
3.1.
Python
4.
When and Why to Use Closures
4.1.
Creating function factories
4.2.
Python
4.3.
Encapsulating state
4.4.
Python
4.5.
Implementing decorators
4.6.
Python
5.
Frequently Asked Questions
5.1.
What is a closure in Python?
5.2.
When should I use a closure?
5.3.
What makes a closure different from a plain function?
6.
Conclusion
Last Updated: Jul 31, 2024
Easy

What are Python Closures?

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Closures are a fundamental concept in programming that enhance data encapsulation and function flexibility. They provide a way for a function to access its external environment, retaining a link to variables from an outer function even after that function has completed execution. 

This ability makes closures particularly valuable for creating function factories and dealing with asynchronous code. 

What are Python Closures?

In this article, we will learn how closures function, their significance in practical coding scenarios, and their implementation through nested functions, with the help of examples.

Nested functions in Python

In Python, you can define functions inside other functions. These are called nested functions or inner functions. Here's an example of a nested function:

def outer_function(x):
    def inner_function(y):
        return x + y
    return inner_function


In this example, `inner_function` is defined inside `outer_function`. The `inner_function` has access to the variables defined in the outer function's scope, like `x`. When you call `outer_function`, it returns the `inner_function` without calling it.

You can then assign the returned function to a variable and call it later, like this:

  • Python

Python

add_five = outer_function(5)

result = add_five(3)

print(result)
You can also try this code with Online Python Compiler
Run Code


Output: 

8


The `inner_function` remembers the value of `x` from when `outer_function` was called, even though `outer_function` has finished running. This is the basic idea behind closures.

What are Python Closures

A closure is a function object that remembers values in the enclosing scope even if they are not present in memory. In other words, a closure allows the inner function to access variables defined in the outer function even after the outer function has finished running.

For example : 

  • Python

Python

def multiply_by(x):

   def multiply(y):

       return x * y

   return multiply

multiply_by_5 = multiply_by(5)

result = multiply_by_5(3)

print(result)
You can also try this code with Online Python Compiler
Run Code


Output

15


In this example, `multiply_by` is the outer function that takes an argument `x`. Inside `multiply_by`, we define another function called `multiply` that takes an argument `y`. The `multiply` function multiplies `x` and `y` and returns the result.

When we call `multiply_by(5)`, it returns the `multiply` function with `x` set to 5. We assign this returned function to the variable `multiply_by_5`. Now, `multiply_by_5` is a closure that remembers the value of `x` as 5.

We can then call `multiply_by_5(3)`, which executes the `multiply` function with `y` set to 3. The closure remembers that `x` is 5, so it multiplies 5 by 3 and returns the result, which is 15.

Closures are powerful because they allow you to create function templates that can be customized with different values. This can make your code more reusable and easier to read.

When and Why to Use Closures

Closures are useful in several situations. Here are a few common scenarios where closures can be helpful:

Creating function factories

Closures allow you to create functions with customized behavior based on the arguments passed to the outer function. This is useful when you need to create many similar functions with slight variations in their behavior.


Example:

  • Python

Python

def make_greeter(greeting):

   def greet(name):

       return f"{greeting}, {name}!"

   return greet

hello_greeter = make_greeter("Hello")

hi_greeter = make_greeter("Hi")

print(hello_greeter("Rahul"))

print(hi_greeter("Rinki"))
You can also try this code with Online Python Compiler
Run Code

 

Output: 

Hello, Rahul!
Hi, Rinki!

Encapsulating state

Closures can be used to encapsulate state within a function. This is useful when you want to create functions that maintain an internal state without using global variables.

Example:

  • Python

Python

def counter():

   count = 0

   def increment():

       nonlocal count

       count += 1

       return count

   return increment

counter1 = counter()

counter2 = counter()

print(counter1()) 

print(counter1()) 

print(counter2())
You can also try this code with Online Python Compiler
Run Code

 

Output: 

1
1
2

Implementing decorators

Decorators in Python are implemented using closures. Decorators allow you to modify or enhance the behavior of functions without changing their code.
Example:

  • Python

Python

def uppercase_decorator(func):

   def wrapper():

       result = func()

       return result.upper()

   return wrapper

@uppercase_decorator

def greet():

   return "hello, world!"

print(greet())
You can also try this code with Online Python Compiler
Run Code

 

Output: 

HELLO, WORLD!


Closures are a powerful feature in Python that enable you to write more concise and expressive code. They are particularly useful when you need to create functions with customized behavior, encapsulate state, or implement decorators.

Frequently Asked Questions

What is a closure in Python?

A closure is a function object that remembers values in enclosing scopes even if they are not present in memory.

When should I use a closure?

Use closures to avoid the use of global variables and to implement data hiding and encapsulation in situations where objects are too heavyweight.

What makes a closure different from a plain function?

Unlike a plain function, a closure "closes over" the variables from its environment, maintaining access to these variables even after the outer function has finished executing.

Conclusion

In this article, we have learned the fundamentals of closures, how they preserve the execution environment, and their practical applications. We've discussed the nested functions as a way to implement closures and explained why the knowledge of closures can significantly enhance your programming skills in Python.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass