Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Hello everyone, I am a bowl week, a front end that does not want to be drunk (inrolled). If I am lucky enough to write an article that you like, I am very lucky

A decorator

Decorator: Literally, a device that decorates an object. You can add new functionality to decorated objects or impose restrictions or help output without modifying the original code.

The features of decorators are that functions appear as arguments, and decorators have the features of closures. The sample code looks like this:

Def class (func): def wrapper(): Func () print(" add student to school list ") print(" add student to department list ") print(" add student to class list ") return wrapper @class def student(): Print (" I am my flower ") student() -- Print result -- ADD student to school student list Add student to Department student list Add student to class listCopy the code

Decorates a function with the **@**** symbol and the function name **

Execution flow: Because student is the decorated function, the system passes the student function as an argument to the decorate function, executes the function, and assigns the return value to the student function.

The previous code is equal to the following code:

Def class (func): def wrapper(): Func () print(" add student to school list ") print(" add student to class list ") return wrapper def student(): Print (" I'm decorate ") # pass the return value to f and call f = class (student) # Otherwise, call f() "" -- output result -- I am Flower Added student to School student List Added student to Department student List Added student to Class list" "Copy the code

If there is a directly executable statement outside the student function, it will be executed without calling the student function, as shown in the following code:

Def decorate(func): print(" this is external code ") def wrapper(): Func () print(" add student to school list ") print(" add student to department list ") print(" add student to class list ") return wrapper @class def student(): Print (" I am a little flower ") # student () ' ' '- the output - it is the external code' ' 'Copy the code

Application scenarios

Can be used for e-commerce site to judge whether the user login to continue to execute; Example code for adding logs and other scenarios is as follows:

Def class (func): def wrapper(): Func () print(" checking user login ") # if # code block for checking user login # pass print(" user login ") return wrapper @decorate # def Add_shopping_cart (): print(" add successfully ") @decorate # def payment(): print(" add successfully ") Print (" payment succeeded ") add_shopping_cart() payment() "-- print result -- Add succeeded checking whether user has logged inCopy the code

Universal decorator

Since the parameters of a function may be unfixed, you can do this with the function’s mutable parameters.

Example code is as follows:

Def class (func): def wrapper(*args, **kwargs): ) print("............." ) print(" check done ") func(*args, **kwargs) return wrapper @decorate # def f1(): Print (" class def student(*students) @class def f2(name): print(" class def student(*students): Print (ch) @class def student_classroom(*students, classroom=" class "): print(ch) @class def student_classroom(*students, classroom=" class "): Print (f" this is {classroom} student ") for ch in students: print(ch) . Check out This does not have any function "' f2 (" a bowl of weeks") ' ' '- the output - is under testing... . Check out The name is: a bowl of week "' student (" zhang", "li si", "detective") ' ' '-- - the output - is under testing... . Class_classroom (" class_classroom ", "class_classroom "," class_classroom ", "class_classroom "," class_classroom ") . That's it. This is the front end class.Copy the code

To prevent errors, set the decorator to universal when defining it

Multilayer decorator

The order of execution is from the inside out, with the innermost decorator called first and the outermost decorator called last, as shown in the following example code:

Def maths(func): # def wrapper(*args, **kwargs): Func (*args, **kwargs) print(*args, **kwargs) return wrapper def Chinese(func): Func (*args, **kwargs) print(" kwargs ") return wrapper def English(func): Func (*args, **kwargs) print(" the student has been learning English ") return wrapper @mathlete@english def student1(name): Print (f" student {name} already done ") @english @chinese@maths def student2(name): Print (f" student {name} done ") # call the function student1(" student2 ") # print(f" student {name} done ") # call the function student1(" student2 ") # print(f" student {name} done ") # call the function student1(" student1 ") # print(f" student {name} done ") # call the function student1(" student2 ") # print(f" student {name} done ") # The student has studied math the student has studied Chinese the student has studied English.Copy the code

Decorator with parameters

Decorators with parameters are divided into three layers as follows:

The first layer is responsible for receiving the parameters of the decorator

The second layer is responsible for receiving functions

The third layer is responsible for receiving the parameters of the function

The sample code looks like this:

Def outer(a): # outer(a): def wrapper(*args, **kwargs): For I in range(a): Print (I) func(*args, **kwargs) return wrapper # outer(3) def number(): Print (" print finished ") number() "0 1 2"Copy the code

The outermost function is responsible for receiving the decorator arguments, with the contents of the original decorator.