python--decorators (two different usage scenarios)

Source: Internet
Author: User

Tag:__name__   ret   error       ...    orm   for   false    support    

#例子, add validation function to Show_name () function by adorner #version 1  #普通函数不带参数, adorner with parameter Def auth (func):     # print (func.__name__)   #通过打印这个结果得知, Show_name function was passed in     def auth_decorator ( USER, PWD):        u =  "Toby"          p =  "1qaz#edc"         if user  == u:            if pwd ==  p:                print ( "Auth done ...")                  func ()   #所以形式参数func等价于show_name ()              else:                 Print ("password Error ")         else:             print ("Not found user")     return auth_ Decorator@authdef show_name ():     print ("My name is toby") Show_name (" Toby "," 1qaz#edc ") # ###################################################################################### # # # #version  2  common functions and adorners with parametric Def auth (func):     # print (func.__name__)   #通过打印这个结果得知, the Show_name function was passed in     def auth_decorator (NAME, USER, PWD):         u =  "Toby"          p =  "1qaz#edc"         if user == u:             if pwd == p:        &Nbsp;        print ("Auth done ...")                  func (name)   #所以形式参数func等价于show_name ()             else:                 print ("Password error")          else:             print ("Not found user")     return auth_decorator@authdef show_ Name:     print ("my name is {0}". Format (name)) Show_name ("TTR",  " Toby "," 1qaz#edc ") #此时就要把show_name函数看成是show_name (NAME, USER, PWD) So, pass three parameters in # #################### ##################################################################### #version  3  The syntax used below, Allows dynamic creation of a decorated function object Def auth (func): &NBSp;   # print (func.__name__)   #通过打印这个结果得知, Show_name function was passed in     def  auth_decorator (USER, PWD):        u =  "Toby"         p =  "1qaz#edc"          if user == u:             if pwd == p:                 print ("Auth done ...")                  func ()   #所以形式参数func等价于show_name ()              else:                 print ("Password error")         else :              print ("Not found user")     return  auth_decoratordef show_name ():     print ("My name is juck") # In this function with the auth_enable parameter to control whether to decorate the Show_name function #auth_enable parameter is true, you must provide the user and pwd# is said to be open authentication and shutdown verification by Auth_enable to decide def  Show_name_strengthen (auth_enable=false, u=none, p=none ):     if auth_ Enable:        s = auth (Show_name)          s (u, p)     else:         show_name () #show_name_strengthen () Show_name_strengthen (true,  "Toby",  "1qaz#edc") ' two different grammars, Use the scene:     1, syntax with the @ symbol to use the adorner, this syntax is often used to make a permanent change to different functions     2, This syntax allows us to dynamically create a decorated function object, such as an adorner with several different functions,     introducing a judging condition to use a different adorner, which means that multiple optional behaviors can be supported.


python--decorators (two different usage scenarios)

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.