What function can be called a closure function? It mainly satisfies two points: functions defined inside functions; external variables but not global variables are referenced.
A python decorator is essentially a function, which allows other functions to add additional functions without any code changes. The return value of the decorator is also a function object (a pointer to a function)
The outer function of the decorator function passes in the name of the function I want to decorate, and returns the name of the decorated function; the inner function (closure) is responsible for decorating the decorated function. From the above description, we need to remember a few properties of the decorator for a better understanding later:
Essence: is a function
Parameters: is the name of the function you want to decorate ( not function call )
Return: is the decorated function name (also not a function call )
What it does: Add extra functionality to an already existing object
Features: No need to make any code changes to the object
There are many classic application scenarios for python decorators, such as log insertion, performance testing, transaction processing, permission verification, etc. Decorators are a great design to solve this kind of problem. And from the introduction, we can also conclude that the biggest role of the decorator is that for the programs we have written, we can extract some similar code to form multiple decorators with specific functions, so that we can target Different requirements use specific decorators. At this time, because the source code removes a lot of generalized content, the source code has a clearer logic .
function decorator
As an example of adding a timing
import time
def decorator(func): def wrapper(*args, **kwargs): start_time = time.time() func() end_time = time.time() print(end_time - start_time) return wrapper @decorator def func(): time.sleep(0.8) func() # 函数调用
# 输出:0.800644397735595
In the above code func is the function that I want to decorate, I want to use the decorator to display the time when the func function runs. @decorator This syntax is equivalent to executing func = decorator(func), decorating and returning the func function. Let's take a look at our decorator function - decorator, the incoming parameter of this function is func (decorated function), and the return parameter is the inner function. The inner function - wrapper here is actually equivalent to a closure function, which plays the role of decorating a given function. The wrapper parameters are *args, **kwargs . The parameters represented by *args are passed in in the form of a list; the parameters represented by **kwargs are passed in in the form of a dictionary:
All parameters in the form of key=value are stored in kwargs, and all the remaining parameters are stored in args in the form of a list. It should be noted here: in order not to destroy the logic of the original function, we must ensure that the incoming parameters and return value types of the inner function wrapper and the decorated function func must be consistent.