Dremendo Tag Line

Closures in Python Programming

Function in Python

In this lesson, we will understand what is Closures in Python Programming and how to create them along with some examples.

What are Closures in Python Programming?

Closures in python are nested or inner functions whose return value depends upon the value of the variables declared in the outer or enclosing function. It is a function object that remember the values of the variables declared inside the enclosing or outer function even after deleting the memory of the outer function.

video-poster

Closures Example 1

# this is outer function or enclosing function
def alpha(data):

    # this is closures function (inner function)
    def beta():
        # the inner function beta accessing and printing the alpha function's variable 'data'
        print(data)

    return beta     # returning the memory address of the inner function


x = alpha('Hello World')
y = alpha('Hello Python')
x()
y()

Output

Hello World
Hello Python

In the above program, we have declared a function called alpha and a nested function called beta. The beta function access and print the alpha function's variable data on the screen using print function. On line number 11 and 12 we have created two variables x and y that points to the memory address of the closer function beta having different inputs. On line number 13 and 14 we are calling the closure function using variable x and y.

Closures Example 2

# this is outer function or enclosing function
def power(p):

    # this is closures function (inner function)
    def base(b):
        print(pow(b,p))

    return base     # returning the memory address of the inner function

square = power(2)
cube = power(3)

square(2)
square(3)
cube(2)
cube(3)

Output

4
9
8
27

In the above program, we can see that the result of inner function base depends upon the value of the variable p which is declared inside the scope of the outer function power.

Closures Example 3

# this is outer function or enclosing function
def calculate():

    x = 100

    # this is closures function 1
    def first():
        nonlocal x
        x = x * 2
        return x

    # this is closures function 2
    def second():
        nonlocal x
        x = x * 4
        return x

    return first, second    # returning the memory address of both the inner functions

x, y = calculate()

print(x())
print(y())

Output

200
800

The above example shows how to define and use multiple closures in a program.

Closures Example 4

# this is outer function or enclosing function
def power(p):

    # this is closures function (inner function)
    def base(b):
        print(pow(b,p))

    return base     # returning the memory address of the inner function


square = power(2)
del power    # deleting the memory address of the enclosing function
square(2)
square(3)

Output

4
9

From the above program, it is clear that once we store the address of the closure function in a variable, we can access it even if we delete the memory address of the enclosing function.

Where can we use Closures?

Closures can be used in place of a class that contains a few or generally one method. We can use closures in place of a class because a class can also remember the values of its enclosing member variables just like the closures does.