in Python by
Q:
What Are Closures In Python?

1 Answer

0 votes
by
Python closures are function objects returned by another function. We use them to eliminate code redundancy.

In the example below, we’ve written a simple closure for multiplying numbers.

def multiply_number(num):

    def product(number):

        'product() here is a closure'

        return num * number

    return product

num_2 = multiply_number(2)

print(num_2(11))

print(num_2(24))

num_6 = multiply_number(6)

print(num_6(1))

The output is:

22

48

6
Click here to read more about Python
Click here to read more about Insurance

Related questions

0 votes
asked Jan 11 in Python by SakshiSharma
0 votes
asked Aug 30, 2020 in Python by sharadyadav1986
0 votes
asked Jun 30, 2020 in Python by GeorgeBell
0 votes
asked Jun 26, 2020 in Python by AdilsonLima
0 votes
asked May 17, 2020 in Python by SakshiSharma
...