What is Function Closure in Python

Posted on

In Python programming, closures are a fundamental concept that every developer should understand. So, what exactly is a closure in Python? A closure is a function object that retains a reference to the variables from its parent scope, even when it’s called outside that scope. This ability enables closures to remember the values of those variables, creating a self-contained and versatile chunk of code. In other words, closures are a way of creating functions that have access to variables that are not in their local scope.

Understanding closure in Python is crucial for writing efficient and powerful Python programs. In the following sections of this article, we’ll dive deeper into closures and explore how they can be used in Python programming. We’ll provide examples, discuss criteria, and highlight advantages of using closures. By the end of this article, you’ll have a better understanding of closure in Python and be able to utilize this powerful programming concept in your own projects.

Python Closures and Decorators

Closures in Python are powerful tools that can be used to create self-contained and versatile functions. However, when combined with decorators, closures become even more powerful and can modify the behavior of other functions.

Decorators are functions that take another function as an input and return a new modified function as output. They are used to add functionality to an existing function without modifying its source code. When closures are used in conjunction with decorators, developers can create dynamic code generation, allowing for even more flexibility and functionality.

Python Closures and Decorators Example

For example, let’s say we have a function that performs a time-consuming operation, such as computing the factorial of a large number. We can use a closure to cache the results of this function to avoid recomputing the same thing repeatedly. We can then use a decorator to add this caching functionality to any function that needs it.

Function NameDescription
cachedA decorator function that adds caching functionality to a given function.
factorialA function that computes the factorial of a number.

Here’s an example:

def cached(func):
    cache = {}
    def wrapper(*args):
        if args not in cache:
            cache[args] = func(*args)
        return cache[args]
    return wrapper

def factorial(n):
    if n == 0:
        return 1
        return n * factorial(n-1)

In this example, we define a decorator function called “cached” that takes a function as input and returns a new function that caches the results of the original function. We then define a function called “factorial” that computes the factorial of a number.

We decorate the “factorial” function with the “@cached” decorator, which adds caching functionality to the function. Now, when we call “factorial,” the cached version of the function is called, which retrieves previously computed results from the cache instead of recomputing them.

This is just one example of how closures and decorators can be used in Python. By combining these powerful tools, developers can create functions that are both dynamic and efficient.

When to Use Closures in Python

As we’ve seen in the previous sections, closures are incredibly powerful programming tools that can be used in a variety of situations. But when should we actually use them in our Python programs?

Closures are particularly useful when we want to encapsulate data and behavior within a function. They enable us to store data in a private scope and prevent it from being accessed or modified by other parts of our code. Additionally, closures can be used to create custom functions with preset arguments, making our code more modular and reusable.

One specific scenario in which closures are often used is when implementing memoization. Memoization is a programming optimization technique that involves storing the results of expensive function calls and returning the cached result when the same inputs occur again. Closures can be used to store the cached results in a private scope, preventing them from being accessed or modified by other parts of our code.

Another scenario in which closures can be useful is when we want to maintain state across multiple function calls. For example, we might want to create a counter that keeps track of how many times a function has been called. Closures enable us to store the counter in a private scope and update its value every time the function is called.

Examples of Closures in Python

To better understand how closures work in Python, let’s dive into several practical examples:

Example 1: Maintaining a Counter



def counter():
    count = 0
    def inner():
        nonlocal count
        count += 1
        return count
    return inner

increment = counter()


This example uses a closure to maintain a counter that can be incremented each time the function is called. The inner function “remembers” the value of the count variable from its parent scope, allowing it to persist and increment with each call to the outer function.

Example 2: Implementing a Cache



def cache(func):
    memory = {}
    def wrapper(*args):
        if args in memory:
            return memory[args]
        result = func(*args)
        memory[args] = result
        return result
    return wrapper

def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)



This example uses a closure to implement a cache for the Fibonacci sequence function. The outer function “cache” takes a function as input and returns a new function that caches the results of the original function. The inner function “wrapper” checks if the arguments passed to the function have been seen before; if so, it returns the cached result instead of recomputing it. This saves time and resources by avoiding redundant computations.

Example 3: Creating Custom Functions with Preset Arguments



def multiply(x):
    def inner(y):
        return x * y
    return inner

double = multiply(2)
triple = multiply(3)


This example uses a closure to create custom functions with preset arguments. The outer function “multiply” takes an argument x and returns a new function “inner” that takes another argument y and multiplies it by x. This allows for the creation of new functions with different preset values for x, enabling more modular and reusable code.

Criteria for Closure in Python

In order for a function to be considered a closure in Python, it must meet certain criteria. These criteria are:

Accesses a variable defined outside the functionA closure must be able to access a variable defined in its enclosing environment.
References the variableThe closure must reference the variable defined outside the function so that it can remember its value.
Is passed as a valueThe closure is passed as a value to another function or returned as a result.

It is important to meet these criteria in order to create and use closures effectively in your Python programs. By meeting these criteria, you can create self-contained functions that remember values from their parent scopes, enabling you to build powerful and modular programs.

Advantages of Closure in Python

Closures offer several advantages in Python programming. By utilizing closures, we can create functions with persistent state, allowing for increased modularity and reusability. This means that closures can be defined once and used many times, reducing duplicate code and increasing efficiency.

Closures can also improve performance by reducing the need for global variables. By encapsulating data and behavior within a function, closures make it possible to create self-contained code that does not rely on external variables. This can lead to cleaner, more efficient code that is easier to understand and maintain.

Another advantage of closures is their ability to enable efficient memory utilization. With closures, we can create functions that remember variables from their parent scopes, without having to store those variables globally or pass them as arguments. This can result in significant memory savings, especially in large-scale applications.


In conclusion, closures are a powerful programming concept in Python that offer many advantages for building efficient and versatile code. By understanding the criteria, examples, and advantages discussed in this article, we can leverage closures to create better programs and improve our development practices.


Understanding closures in Python is essential for writing efficient and powerful code. By leveraging closures, we can create functions with persistent state and remember variables from parent scopes, resulting in increased modularity and reusability.

Combining closures with decorators allows developers to generate dynamic code and add additional functionality to existing functions. Additionally, closures offer several advantages, including improved performance and efficient memory utilization.

When deciding when to use closures, it’s important to consider their criteria, including the function being nested and referencing variables from the enclosing scope.

Overall, closures are a valuable tool for solving real-world programming challenges and creating elegant and versatile programs. Start incorporating closures in your Python projects today to unlock their full potential.

Leave a Reply

Your email address will not be published. Required fields are marked *