zoukankan      html  css  js  c++  java
  • 初学Python——装饰器

    一、什么是装饰器

    当我们做好一个产品之后,需要对它进行不断地维护,对某些函数增加一些功能。这个时候如果去修改源代码将是非常不合适的。(原因:1.原则上已经写好的函数尽量不去修改它,因为一旦修改可能会导致不可预知的错误发生或者降低稳定性。2.函数可能被调用很多很多次,如果修改函数有可能会导致调用方式发生改变,会有大量的修改工作。)所以,装饰器就出现了。它可以做到为函数增添新功能的同时而不修改函数本身的源代码。

    二、装饰器的原则

    1.不能修改被修饰函数的源代码

    2.不能修改被修饰函数的调用方式

    三、实现装饰器的知识储备

    1.函数即“变量”:定义一个函数,相当于将函数体赋值给函数名这个变量。

    2.高阶函数:满足以下两个条件之一的函数即为高阶函数:①把函数名当做实参传给另一个函数②返回值中包含函数名

    3.嵌套函数:函数是可以嵌套的

    四、使用装饰器

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

    装饰器的本质是函数,功能是为其它函数添加附加功能

    #现在有两个函数,分别有各自的功能,需求是计算每个函数运行的时间
    
    def text1():
        time.sleep(2)
        print("this is text1")
    
    def text2(*args,**kwargs):
        time.sleep(2)
        print("this is text2",args[0],args[1])

    可以写装饰器:

    def timer(func):
        def deco(*args,**kwargs):
            start_time=time.time()
            func(*args,**kwargs)
            end_time=time.time()
            print("the function tun time is {0}".format(end_time-start_time))
        return deco

    其中func()函数是原函数真正的执行过程,deco函数内其他的语句都是新添加功能的语句

    装饰器名叫timer

    写好装饰器就行了吗?不是,还差一个步骤,在原函数上方增加一条语句,整合到一起:

    def timer(func):
        def deco(*args,**kwargs):
            start_time=time.time()
            func(*args,**kwargs)
            end_time=time.time()
            print("the function tun time is {0}".format(end_time-start_time))
        return deco
    
    @timer  #此操作的作用是将装饰器的功能加到原函数上 相当于text1=timer(text1)
    def text1():
        time.sleep(2)
        print("this is text1")
    
    
    @timer #没有@timer语句则不会将装饰器功能加上
    def text2(*args,**kwargs): #如果原函数有参数,需要在装饰器中deco上加参数
        time.sleep(2)
        print("this is text2",args[0],args[1])
    
    text1() #不改变调用方式,且增加了新功能
    text2(29000,"hello")

    装饰器解析:deco函数用来实现新增功能,timer函数和@timer语句用来使调用方式不发生改变

    执行结果:

    五、带参数的装饰器

    这里说的不是原函数带参数,而是装饰器函数本身带参数

    现在有需求,用装饰器实现登陆过程,并且有两种不同的登陆方式:使用本地数据登陆和使用云端数据登陆

    #现在有三个网页,其中登陆home时用本地数据,登陆bbs时用云端数据
    
    def index():
        print("welcome to index page")
    
    def home():
        print("welcome to home page")
        return "form home"
    
    def bbs():
        print("welcome to bbs page")

    需要装饰器:

    name="alex"
    word="123"
    def auth(auth_type):
        def auth_out(func):
            def deco(*args,**kwargs):
                if auth_type=="local": #如果使用本地数据
                    uesrname=input("请输入用户名:").strip()
                    password=input("请输入密码:").strip()
                    if uesrname==name : # 如果用户存在
                        if password==word:#如果密码正确
                            print("使用本地数据")
                            print("欢迎进入!")
                            return func(*args,**kwargs) #进入home界面
                        else:
                            print("密码错误")
                    else:
                        print("用户不存在")
                elif auth_type=="cloude": #如果使用云端数据
                    print("使用云端数据")
                    print("欢迎")
                    func() # 进入bbs界面
            return deco
        return auth_out
    
    def index():
        print("welcome to index page")
    
    @auth(auth_type="local") #登录home界面使用本地用户数据,在auth()内传参
    def home():
        print("welcome to home page")
        return "form home"
    
    @auth(auth_type="cloude") #登录bbs界面使用云端用户数据
    def bbs():
        print("welcome to bbs page")
    
    index()
    print(home())
    bbs()

    其实就是将原来的装饰器外层又嵌套了一层函数,用来传递参数。@语句也变成了@auth(auth_type=" ")

    运行结果:

  • 相关阅读:
    Day Six(Beta)
    Day Five (beta)
    Day Four(Beta)
    Day Three(Beta)
    Day Two(Beta)
    Day One(Beta)
    项目冲刺——总结
    beta版本贡献率
    软件工程实践总结
    团队作业--Beta版本冲刺
  • 原文地址:https://www.cnblogs.com/V587Chinese/p/8953774.html
Copyright © 2011-2022 走看看