zoukankan      html  css  js  c++  java
  • 装饰器

    定义:本质是函数,(装饰其他函数)就是为其他函数添加附加功能
    原则:
    1.不能修改被装饰的函数的源代码
    2.不能修改被装饰的函数的调用方法

    实现装饰器知识储备
    1.函数即“变量”
    2.高阶函数 

      a.把一个函数名当做实参传给另外一个函数
      b.返回值中包含函数名(不修改函数的调用方式)

    3.嵌套函数
    高阶函数+嵌套函数==>装饰器

    例子:

    import time
    def timer(func):
        def warpper():
            start_time = time.time()
            func()
            stop_time = time.time()
            print("the fun time is %s"%(stop_time - start_time))
        return  warpper
    @timer  #相当于 test1 = warpper(test1)
    def test1():
        time.sleep(1)
        print("sleep in test1")
    
    test1()
    装饰器基础版
    import time
    def timer(func):
        def warpper(*args,**kwargs):#*args,**kwargs可以通用于有不同参数传递
            start_time = time.time()
            func(*args,**kwargs)#*args,**kwargs可以通用于有不同参数传递
            stop_time = time.time()
            print("the fun time is %s"%(stop_time - start_time))
        return  warpper
    @timer  #相当于 test1 = warpper(test1)
    def test1():
        time.sleep(1)
        print("sleep in test1")
    @timer
    def test2(name):#因为这里有传递的参数,原来的装饰器不适用,必须增加对应的参数,而为了适应不同的参数传递,所以上方使用args,**kwargs
        time.sleep(1)
        print("name:",name)
    
    test1()
    test2("GaShuaiGe")
    装饰器中级版

     带return后,会将函数的结果也返回

    import  time
    def decorator(func):
        def warper(*args,**kwargs):
            start_time = time.time()
            res = func()
            stop_time = time.time()
            print("in the warper func time is %s"%(stop_time-start_time))
            return res #这个是用来返回函数结果,若没有这个home()里面return "from home"没有
        return  warper
    
    
    def index():
        print("this is index page")
    @decorator
    def home():
        time.sleep(1)
        print("this is home page")
        return "from home"
    @decorator
    def bbs():
        time.sleep(1)
        print("this is bbs page")
    
    index()
    print("index()",index())
    home()
    print("home()",home())
    bbs()
    print("bbs()",bbs())
    带return的装饰器中级版

     高级版:再嵌套一层

    import  time
    user , passwd = "alex","123"
    def decorator(decorator_type):#将local以及ssh传入
        #print(decorator_type)
        def outwarper(func):
            def warper(*args, **kwargs):
                print("*args, **kwargs",*args, **kwargs)
                if decorator_type == "local":
                    username = input("username:").strip()
                    password = input("password:").strip()
                    if username == user and passwd == password:
                        res = func(*args, **kwargs)
                        print("33[31;1mwelcome!33[0m")
                        return res
                    else:
                        print("--------------------------")
                        exit("33[33;1m用户名密码错误!33[0m")
                elif decorator_type =="ssh":
                    print("----------------")
                    exit("33[33;1m没有ssh!33[0m")
    
            return warper
        return outwarper
    
    def index():
        print("this is index page")
    @decorator(decorator_type = "local")#decorator_type = "local"指定不同的方式
    def home():
        time.sleep(1)
        print("this is home page")
        return "from home"
    
    @decorator(decorator_type = "ssh")
    def bbs():
        time.sleep(1)
        print("this is bbs page")
    
    
    index()
    #print("index()",index())
    home()
    #print("home()",home())
    bbs()
    #print("bbs()",bbs())
    装饰器高级版
  • 相关阅读:
    day50——前端简介、标签分类、常用标签
    day46——约束条件、表与表建 关系、修改表的完整语法
    day45——存储引擎、数据类型、约束条件
    day44——存储数据的发展、数据库分类、mysql
    Ⅰ:计算机核心基础
    Ⅶ:基本数据类型及内置方法
    Ⅶ:作业
    Ⅵ:深浅copy
    Ⅵ:流程控制
    Ⅳ:运算符
  • 原文地址:https://www.cnblogs.com/cheng662540/p/8059822.html
Copyright © 2011-2022 走看看