zoukankan      html  css  js  c++  java
  • python 高阶函数与装饰器

    高阶函数定义
    1.函数接收的参数是一个函数名
    2.函数的返回值是一个函数名
    以上两者满足任意一个,就是高阶函数

    装饰器定义
    本质就是函数,功能是为其他函数添加新功能
    装饰器的原则

       1.不修改被装饰函数的源代码(开放封闭原则)

       2.为被装饰函数添加新功能后,不修改被修饰函数的调用方式

       装饰器=高阶函数+函数嵌套+闭包

       

    # 无返回值无参数
    import time
    
    def timer(func):     #func = test
        def w():
            start_time = time.time()
            func()     #就是在运行test()
            stop_time = time.time()
            print("运行时间是%d"%(stop_time-start_time))
        return w
    
    @timer    # 相当于 test = timer(test)
    def test():
        time.sleep(2)
        print("from test")
    
    # test = timer(test)  #返回的是w的地址
    # test()       #相当于执行w
    
    test()
    
    #加上返回值
    import time
    
    def timer(func):       #func = test
        def w():
            start_time = time.time()
            res = func()    #就是在运行test()
            stop_time = time.time()
            print("运行时间是%d"%(stop_time-start_time))
            return res
        return w
    
    @timer    # 相当于 test = timer(test)
    def test():
        time.sleep(2)
        print("from test")
        return "这是test的返回值"
    
    # test = timer(test)  #返回的是w的地址
    # test()       #相当于执行w
    
    res = test()
    print(res)
    
    #加上参数和返回值 装饰器最终形式
    import time
    
    def 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("运行时间是%d"%(stop_time-start_time))
            return res
        return w
    
    
    @timer    # 相当于 test = timer(test)
    def test(name,age):
        time.sleep(2)
        print("from test   %s   %s"%(name,age))
        return "这是test的返回值"
    
    
    res = test("liao",18)
    print(res)
    
    @timer     # 相当于 test1 = timer(test1)
    def test1(name,age,g):
        time.sleep(1)
        print("from test1   %s   %s  %s"%(name,age,g))
        return "这是test1的返回值"
    
    res1 = test1("bo",26,"shi")
    print(res1)
    

    用户登陆(简单流程判断)

    l = [{"name":"liao","pwd":"123"},{"name":"tom","pwd":"123"}]     #用户数据
    c_d = {"user":None,"login":False}          #定义一个空的临时的用户字字典
    
    def a_f(func):
        def w(*args,**kwargs):
            if c_d["user"] and c_d["login"]:      #判断临时字典里是否有用户登陆,没有就输入
                res = func(*args,**kwargs)        #有就进入下一步
                return res
    
            user = input("请输入用户名:").strip()   #临时字典没有数据就输入用户名
            pwd = input("请输入密码:").strip()      #临时字典没有数据就输入密码
    
            for i in l:          #遍历用户数据
                if user == i["name"] and pwd == i["pwd"]:    #判断输入的用户和密码是否在用户数据里
                    c_d["user"] = user        #输入正确,数据保存到临时的用户字字典里,下一步不用再输入用户和密码
                    c_d["login"] = True
                    res = func(*args,**kwargs)    #进入
                    return res
            else:                       #如果输入的用户名和密码不在用记数据里,提示用户
                print("用户名或者密码错误")
        return w
    
    @a_f
    def index():
        print("欢迎来到主页面")
    
    @a_f
    def home():
        print("这里是你家")
    
    @a_f
    def shopping_car():
        print("查看购物车啊亲")
    
    index()
    home()
    shopping_car()
    
     

  • 相关阅读:
    C++学习笔记32:泛型编程拓展1
    C++学习笔记31:术语翻译
    Sqrt(x)
    Search a 2D Matrix
    Pascal's Triangle II
    Pascal's Triangle
    Climbing Stairs
    Linux实用命令
    Binary Tree Inorder Traversal
    Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/liaoboshi/p/6142957.html
Copyright © 2011-2022 走看看