Python high-order functions and adorners

Source: Internet
Author: User

Higher-order function definitions
1. The parameter received by the function is a functional name
2. The return value of a function is a functional name
The above two satisfies either one, is the higher order function

Adorner definition
The essence is the function, the function is to add new functions for other functions
The principle of adorners

1. Do not modify the source code of the decorated function (open closed principle)

2. When a new function is added to the decorated function, the method of calling the modified function is not modified

adorner = higher order function + function nesting + closure

# no return value no parameter import timedef timer (func):     #func = Test    def w ():        start_time = Time.time ()        func ()     # is to run test ()        stop_time = Time.time ()        print ("Run time is%d"% (stop_time-start_time))    return W@timer    # equals Test = timer (test) def test ():    time.sleep (2)    print ("From Test") # test = timer (test)  #返回的是w的地址 # Test ()       #相当于执行wtest ()

#加上返回值import timedef Timer (func):       #func = Test    def w ():        start_time = time.time ()        res = func ()    #就是在运行test ()        stop_time = Time.time ()        print ("Run time is%d"% (stop_time-start_time))        return res    return W@timer    # equivalent to test = Timer (test) def test ():    time.sleep (2)    print ("from Test")    return " This is the return value of test "# test = timer (test)  #返回的是w的地址 # Test ()       #相当于执行wres = Test () print (res)

#加上参数和返回值 Adorner final Form import timedef timer (func):          #func = Test   #func = Test1    def w (*args,**kwargs):        Start_ Time = Time.time ()        res = func (*args,**kwargs)     #就是在运行test ()   test1 ()        stop_time = Time.time ()        Print ("Run time is%d"% (stop_time-start_time))        return res    return W@timer    # equivalent to test = Timer (test) def test (name,age):    time.sleep (2)    print ("from Test   %s   %s "% (name,age)) return    " This is the return value of test "res = Test (" Liao ") print (res) @timer     # equivalent to Test1 = Timer (test1) def test1 (name,age,g):    time.sleep (1)    print ("From Test1   %s   %s  "% (Name,age, g)    Return "This is the return value of test1" res1 = Test1 ("bo", "Shi") print (RES1)

User login (simple process judgment)

L = [{"Name": "Liao", "pwd": "123"},{"name": "Tom", "pwd": "123"}] #用户数据c_d = {"User": None, "Login": False} #定义一个空的临时            User Word dictionary def a_f (func): Def w (*args,**kwargs): If c_d["user"] and c_d["Login"]: #判断临时字典里是否有用户登陆, no input res = func (*args,**kwargs) #有就进入下一步 return res user = input ("Please enter user name:"). Strip () #临时字典没有数据就输入用 User name pwd = input ("Please enter password:"). Strip () #临时字典没有数据就输入密码 for I in L: #遍历用户数据 if user = = i["n Ame "] and pwd = = i[" pwd "]: #判断输入的用户和密码是否在用户数据里 c_d[" user "] = user #输入正确, data is saved to a temporary user word dictionary, the next step no longer input user and password c_d["login"] = True res = func (*args,**kwargs) #进入 return res E LSE: #如果输入的用户名和密码不在用记数据里, prompting user print ("Username or password error") return W@a_fdef index (): Print ("Welcome Come to the main page ") @a_fdef Home (): Print (" Here is your Home ") @a_fdef Shopping_car (): Print (" View cart AH Pro ") index () Home () Shopping_car ()


Python high-order functions and adorners

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.