What is Closure in Python & its Use Cases?


In Python, the closer function is not a built-in feature of the language itself. However, in programming, "closures" refer to a powerful concept where a function retains access to the variables from its enclosing scope even after the scope has finished executing. This allows for a form of data encapsulation and can be particularly useful for creating modular and reusable code. Essentially, a closure is created when a function is defined within another function, and the inner function captures and "closes over" the variables from the outer function's scope.

Imagine you have a function outer_function that contains another function inner_function. If inner_function utilizes variables from, even after outer_function has finished executing, those variables remain accessible to inner_function. This encapsulation provides a way to create functions with hidden states, allowing for cleaner code organization and implementation of complex logic. Closures are not only a fundamental aspect of Python programming but also a powerful tool for creating elegant and efficient solutions to various computational problems.

Python Closures

Closures is Python

closures are a powerful concept in which a function retains access to variables from its enclosing scope even after the scope has finished executing. This means that the inner function (the closure) remembers the environment in which it was created and can access variables from that environment, even if the outer function has completed its execution.

Here's a more detailed breakdown

  1. Function Enclosure: When a function is defined within another function, the inner function has access to the variables of the outer function's scope. This is because the inner function is defined within the enclosing scope of the outer function.

  2. Access to Enclosing Scope: Closures allow inner functions to access and "capture" variables from the outer function's scope. These variables are not local to the inner function but are retained in a special data structure known as the function's closure.

  3. Immutable and Mutable Variables: Closures can access both immutable (e.g., numbers, strings, tuples) and mutable (e.g., lists, dictionaries) variables from the enclosing scope. However, when mutable variables are accessed and modified within the closure, the modifications are reflected in the outer scope as well.

  4. Function Object with State: Closures effectively create a function object that "remembers" its environment. Each time the closure is called, it operates with the variables and bindings that were in place when it was defined.

  5. Lifetime Management: Closures keep the variables they capture alive as long as they are needed. This can be particularly useful in scenarios like creating callback functions or maintaining state across multiple function calls.

  6. Example Use Cases: Closures are commonly used in Python for tasks like creating decorators, implementing callback functions, and achieving partial function applications.

In essence, closures provide a way to create functions with a persistent context or state, allowing for more flexible and modular code design. They leverage the dynamic and flexible nature of Python's function objects to encapsulate behavior along with the necessary data.


def outer_function(x):
    # This is the outer function
    def inner_function(y):
        # This is the inner function
        return x + y  # Accessing 'x' from the outer function

    return inner_function  # Returning the inner function

# Example usage of the closure
closure_instance = outer_function(10)
print(closure_instance(5))  # Output will be 15

Key Components of a Closure

  1. Enclosing (Outer) Function: This is the function that contains the nested (inner) function. It defines the environment within which the inner function operates. The inner function can access variables from this enclosing scope.

  2. Nested (Inner) Function: This function is defined within the scope of the outer function. It has access to the variables of the enclosing function, and it can use and modify them even after the outer function has completed execution.

How Do Closures Work?

Closures are functions that capture variables from their enclosing lexical scope (the scope where they are defined) and can access those variables even after the scope has finished executing. This feature allows for powerful and flexible programming constructs. Let's dive deeper into how closures work in Python.

  1. Lexical Scoping: Python uses lexical scoping, which means that a function can access variables defined in its enclosing scope. When a function is defined inside another function, the inner function can access variables from the outer function's scope.
    def outer_function():
        x = 10
        def inner_function():
            print(x)  # Accessing x from the outer scope
        return inner_function
    closure = outer_function()
    closure()  # Output: 10

    In this example, inner_function is a closure because it captures the variable x from its enclosing scope (outer_function). Even though outer_function has finished executing, the value of x is retained by the closure.

  2. Creating Closures: When a function is defined inside another function and references variables from the outer function's scope, Python automatically creates a closure for that inner function.

    def outer_function(x):
        def inner_function():
        return inner_function
    closure1 = outer_function(5)
    closure2 = outer_function(10)
    closure1()  # Output: 5
    closure2()  # Output: 10

    In this example, inner_function captures the value of x from each invocation of outer_function, creating two separate closures with different captured values.

  3. Returning Closures: Functions in Python are first-class citizens, which means they can be passed around as arguments and returned from other functions. This enables the creation and return of closures from functions.

    def multiplier(n):
        def multiply(x):
            return x * n
        return multiply
    double = multiplier(2)
    triple = multiplier(3)
    print(double(5))  # Output: 10
    print(triple(5))  # Output: 15

    In this example, multiplier is a higher-order function that returns a closure (multiply) which multiplies its argument by n. When multiplier is invoked with different values, it creates closures with different multiplication factors.

  4. Modifying Enclosed Variables: Closures can not only access but also modify variables from their enclosing scope if those variables are declared nonlocal.

    def counter():
        count = 0
        def increment():
            nonlocal count
            count += 1
            return count
        return increment
    counter1 = counter()
    print(counter1())  # Output: 1
    print(counter1())  # Output: 2

    In this example, increment modifies the count variable from the enclosing scope counter. By using the nonlocal keyword, increment informs Python that count is not a local variable but a variable from the outer scope.

Use Cases of Closures

  1. Callback Functions: Closures are frequently used to implement callback functions, where a function is passed as an argument to another function and is executed later.
    def perform_operation(x, y, callback):
        result = callback(x, y)
        print("Result:", result)
    def add(x, y):
        return x + y
    def multiply(x, y):
        return x * y
    perform_operation(5, 3, add)        # Output: Result: 8
    perform_operation(5, 3, multiply)   # Output: Result: 15

    Here, perform_operation accepts a callback function that defines the operation to be performed on x and y. Depending on the callback provided, it performs addition or multiplication.

  2. Memoization: Closures can be used for memoization, a technique to cache the results of expensive function calls and return the cached result when the same inputs occur again.

    def memoize(func):
        cache = {}
        def wrapper(*args):
            if args not in cache:
                cache[args] = func(*args)
            return cache[args]
        return wrapper
    def fibonacci(n):
        if n <= 1:
            return n
        return fibonacci(n - 1) + fibonacci(n - 2)
    print(fibonacci(10))  # Output: 55

    In this example, the memoize function creates a closure (wrapper) that caches the results of calls to the fibonacci function. This optimizes the performance by avoiding redundant computations.

  3. Data Hiding and Encapsulation: Closures can be used to create private variables, encapsulating data within a function and preventing direct access from outside.

    def make_counter():
        count = 0
        def increment():
            nonlocal count
            count += 1
            return count
        def get_count():
            return count
        return increment, get_count
    increment_counter, get_counter = make_counter()
    print(increment_counter())  # Output: 1
    print(increment_counter())  # Output: 2
    print(get_counter())        # Output: 2

    Here, make_counter returns two closures: increment for incrementing the counter and get_count for retrieving the current count. The count variable is hidden from outside access.

  4. Partial Function Application: Closures can be used to create functions with predefined arguments, allowing for partial function application.

    def power(base):
        def exponent(exp):
            return base ** exp
        return exponent
    square = power(2)
    cube = power(3)
    print(square(3))  # Output: 9
    print(cube(3))    # Output: 27

    In this example, the power function returns a closure (exponent) that raises the base to the power of the provided exponent. By fixing the base, we create specialized functions for specific powers.


Understanding closure in Python is pivotal for mastering its advanced functionalities. Through closure, Python developers can harness the power of nested functions, enabling the creation of more modular and flexible code structures. By encapsulating the state within a function's scope, closures facilitate data privacy and code organization, enhancing code readability and maintainability. Embracing closure empowers programmers to leverage Python's functional programming capabilities efficiently, leading to more elegant and concise solutions for complex problems.

Similar Articles