Python Tutorial - Closures

Closures, also known as closure functions or closed functions, are actually similar to the nested functions mentioned earlier, except that the external function in the closure does not return a specific value but a function. Under normal circumstances, the returned function is assigned to a variable, which can be executed and called later.

For example, to calculate the nth power of a number, a closure can be written as the following code:

#Closure function, where exponent is called a free variable
def nth_power (exponent):
    def exponent_of (base):
        return base ** exponent
    return exponent_of # The return value is the exponent_of function
square = nth_power (2) # Calculate the square of a number
cube = nth_power (3) # calculate the cube of a number
print (square (2)) # Calculate the square of 2
print (cube (2)) # calculate the cube of 2

The output is:


In the above program, the return value of the external function nth_power is the function exponent_of, not a specific value.

It should be noted that after executing square = nth_power (2) and cube = nth_power (3), the parameter exponent of the external function nth_power () is assigned to square and cube together with the internal function exponent_of, so that square (2) is called afterwards ) Or cube (2), the program can output the result smoothly without error that the parameter exponent is not defined.

Seeing this, readers may ask, why do we need closures? The above program can be written in the following form:

def nth_power_rewrite (base, exponent):
    return base ** exponent

The above program can indeed achieve the same function, but using closures can make the program more concise and readable. Imagine, for example, that you need to calculate the square of many numbers, so which of the following forms does the reader think is better?

# Do not use closures
res1 = nth_power_rewrite (base1, 2)
res2 = nth_power_rewrite (base2, 2)
res3 = nth_power_rewrite (base3, 2)
# Using closures
square = nth_power (2)
res1 = square (base1)
res2 = square (base2)
res3 = square (base3)

Obviously, the second way is more concise. You can input one less parameter each time you call the function.

Second, similar to the advantages of reducing nested functions, the function needs to do some extra work at the beginning. When the function needs to be called multiple times, if the extra work code is placed in an external function, it can reduce unnecessary overhead to improve the efficiency of program operation.

__Closure__ Attribute of Python Closures

Closures have one more __closure__ attribute than ordinary functions, which records the address of a free variable. When the closure is called, the system will find the corresponding free variable according to the address and complete the overall function call.

Take nth_power as an example, when it is called, you can use the __closure__ attribute to get the address stored by the free variable (that is, the exponent parameter in the program), for example:

def nth_power(exponent):
    def exponent_of(base):
        return base ** exponent
    return exponent_of
square = nth_power(2)
#View the value of __closure__
print (square.__closure__)

The output is:

(cell at 0x000001D83C680BE8: int object at 0x00007FF821969360,)

As you can see, the displayed content is an int integer type, which is the initial value of the free variable exponent in square. You can also see that the type of the __closure__ attribute is a tuple, which indicates that a closure can support the form of multiple free variables.


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression

More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises