In-depth analysis of the day4 decorations and in-depth analysis of day4 decorations

Source: Internet
Author: User
Tags python decorator

In-depth analysis of the day4 decorations and in-depth analysis of day4 decorations

Python decorator

The decorator is a common method to add functions to the code without modifying the source code. @ Is the symbol of decoration. We know that when adding a function to the code, we should follow the principle of open and closed. We cannot change the original code at will, so the function of the decorator will be displayed, you only need to add a modifier in front of the function to solve the problem.

Def verification (func): # verification module def inner (): print ("enter your account and password for verification") func () return innerdef f1 (): print (666) def f2 (): print (777) def f3 (): print (888) def f4 (): print (999) f2 = verification (f2) print (f2) f2 ()

In the above Code, we have added a verification function for f2. In many cases, we need to expand the functions of the program without changing the original code. The above is the working principle of the decorator. The decorator is equivalent to adding a code f = func (f) to the function so that the program can verify the code before execution. Let's take a look at the complete decorator.

Def verification (func): # verification module def inner (): print ("enter your account and password for verification") func () return inner def f1 (): print (666) def f2 (): print (777) def f3 (): print (888) def f4 (): print (999) f1 = verification (f1) f2 = verification (f2) f3 = verification (f3) f4 = verification (f4) f2 () # Here f2 is the inner's function address, run f2 () in fact, the execution is inner ()

In the above Code, we have added four pieces of code, so that we can expand the program without modifying the original code, without changing the user habits of other departments, the problem is solved well. This method is actually the principle of the decorator. We can make simple modifications to make the changes look better and more beautiful.

 
Def verification (func ):
# Verification module
Def inner ():
Print ("enter your account and password for verification ")
Func ()
Return inner

@ Verification # f1 = verification (f1)
Def f1 ():
Print (666)

@ Verification # equivalent to f2 = verification (f2)
Def f2 ():
Print (777)

@ Verification # f3 = verification (f3)
Def f3 ():
Print (888)

@ Verification # f4 = verification (f4)
Def f4 ():
Print (999)

F2 () # Here f2 is the inner's function address. Executing f2 () actually executes inner ()
 

In the code above, we applied the modifier @, which also implemented the same functions as the above Code. In fact, the modifier @ verification on function f1 is equivalent to f1 = verification (f1) before proceeding to the function, let's first execute the verification code block. Because verification (f1) is the verification function, the user hasn't called it yet, but the function has been executed, to prevent the function from executing the verification module, we add a layer of function nesting to nest the function at the second layer. The role of the first layer is to return the function name of the second layer nested function, so that the call is performed when the user inputs the call. Therefore, when the user calls the call, the input f2 () is actually equivalent to the execution of the function inner (). The inner () function is also activated and exists in the python memory.

Therefore, if we do not want to execute a function at a certain layer, We will nest a function for the function, return the function name of the first function, and execute the function at the second layer. We can also add parameters to the function. You must know that the function is executing the command.

Pass a parameter to the function and pass a parameter to the function. As we know, when you call f2 (), inner () is actually running, so inner () as many parameters as func. Next we will pass a parameter to the decorator.

Def verification (func): # verification module def inner (name): print ("enter your account and password for verification") func (name) return inner @ verification # f1 = verification (f1) def f1 (name): print (666) @ verification # equivalent to f2 = verification (f2) def f2 (name ): print (777, name) @ verification # f3 = verification (f3) def f3 (name): print (888) @ verification # f4 = verification (f4) def f4 (): print (999) f2 ("alex") # Here, f2 is the inner's function address. Executing f2 () actually executes inner ()

Pass multiple sets of parameters:

Def verification (func ):
# Verification module
Def inner (* args, ** kwargs ):
Print ("enter your account and password for verification ")
Func (* args, ** kwargs)
Return inner

@ Verification # f1 = verification (f1)
Def f1 (name ):
Print (666)

@ Verification # equivalent to f2 = verification (f2)
Def f2 (name ):
Print (777, name)

@ Verification # f3 = verification (f3)
Def f3 (name, age, * args ):
Print (888, name, age, args)

@ Verification # f4 = verification (f4)
Def f4 ():
Print (999)

F2 ("alex") # Here f2 is the inner's function address. Executing f2 () actually executes inner ()
F3 ("alex", "sb", "is", 333,222)

Based on the previously learned dynamic parameter * args, ** kwargs can pass multiple parameters to the function, so that the user can pass multiple parameters without errors.

The running result of the above Code is as follows:

Enter your account and password for verification.
777 alex
Enter your account and password for verification.
888 alex sb ('is', 333,222)
We passed multiple parameters to f3 (), but the system did not report an error and accepted the parameters normally. We can see that using * args, ** kwargs can receive multiple parameters, in addition, different functions are irrelevant even if the number of function parameters is inconsistent.

The modifier is used to extend new functions for existing functions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.