what function can be called a closure function? The main point is to satisfy two points: functions defined inside the function; external variables but non-global variables are referenced.
The python adorner is essentially a function that allows other functions to add additional functionality without any code changes, and the return value of the adorner is also a function object (pointer to the function)
The outer function of the adorner function passes in the name of the function I want to decorate, returns the name of the modified function, and the inner function (closure) is responsible for modifying the modified function. From the above description we need to remember some of the decorator's properties, so that we can better understand the following:
Essence: is a function
Parameter: is the name of the function you want to decorate (and not a function call )
Return: is the decorated function name (also not a function call )
Function: Add additional functionality to an existing object
Features: No code changes are required on the object
Python adorners have many classic applications, such as insert logs, performance tests, transaction processing, permission checks, and so on. Decorators are a great design for solving this kind of problem. And from the introduction of the sake we can also conclude that: the most important role of the adorner is for the program we have written, we can draw out some of the same code to build a number of specific functions of the adorner, so that we can be used for different needs to use a specific adorner, At this time because the source code to remove a large number of generalized content and make the source has more clear logic .
function Adorner
The function adorner is described as an example of adding a timing function to a function.
Import timedef Decorator (func): def wrapper (*args, * *Kwargs): start_time = time.time () Func () End_time =print (End_time-returndef func (): Time.sleep (0.8# Function call
# Output: 0.800644397735595
In the code above , Func is the function I want to adorner, and I want to use the adorner to show when the Func function is running. @decorator This syntax is equivalent to executing func = Decorator (func), Decorating and returning the Func function. Looking at our adorner function- decorator, the function's incoming parameter is func (decorated function), and the return parameter is an inner function. The inner layer function here-wrapper, in fact, is equivalent to the closure function, which plays the role of decorating a given function, the wrapper parameter is *args, **kwargs. The arguments represented by *args are passed in as a list, and the parameters represented by **kwargs are passed in as a dictionary:
All parameters in the form of Key=value are present in the Kwargs, and all remaining parameters are stored in args in the form of a list. It is important to note that in order not to break the logic of the original function, we must ensure that the incoming and return value types of the inner-layer function wrapper and the function func are consistent.
Python practical Note (15) functional programming-decorators