• 函数三


    一、装饰器
        1 开放封闭原则
            软件一旦上线后,就应该遵循开放封闭原则,即对修改源代码是封闭的,对功能的扩展是开放的
            也就是说我们必须找到一种解决方案:能够在不修改一个功能源代码以及调用方式的前提下,为其加上新功能
            总结,原则如下:
                    1、不修改源代码
                    2、不修改调用方式
            目的:
                在遵循1和2原则的基础上扩展新功能

        2、什么是装饰器?
            器指的工具,装饰指的是为被装饰对象添加新功能

            完整含义:
                装饰器即在不修改被装饰对象源代码与调用方式的前提下,为被装饰器对象添加新功能

                装饰器与被装饰的对象均可以是任意可调用的对象

                装饰器=》函数
                被装饰的对象=》函数

            装饰器
            def timmer(func):
                #func=最原始的home
                def wrapper(*args,**kwargs):
                    start_time=time.time()
                    res=func(*args,**kwargs) #调用最原始的home
                    stop_time=time.time()
                    print(stop_time-start_time)
                    return res
                return wrapper

            index=timmer(index) # 新的index=wrapper
            home=timmer(home) #新的home=wrapper

            无参装饰器模板
            def outer(func):
                def inner(*args,**kwargs):
                    res=func(*args,**kwargs)
                    return res
                return inner
        装饰器语法:
                在被装饰的函数上@timmer,必须独占一行且在被装饰的上一行,装饰器也要放在被装饰函数前。
            def timmer(func):
                def wrapper(*args,**kwargs):
                    start_time=time.time()
                    res=func(*args,**kwargs)
                    stop_time=time.time()
                    print(stop_time-start_time)
                    return res
                return wrapper

            @timmer #index=timmer(index)
            def index():
                time.sleep(1)
                print('welcome to index page')
                return 122

            @timmer # home=timmer(home)
            def home(name):
                time.sleep(2)
                print('welcome %s to home page' %name)

            多装饰器叠加:
                import time
                current_user={
                    'username':None,
                    # 'login_time':None
                }

                def auth(func):
                    # func=index
                    def wrapper(*args,**kwargs):
                        if current_user['username']:
                            print('已经登陆过了')
                            res=func(*args,**kwargs)
                            return res

                        uname=input('用户名>>: ').strip()
                        pwd=input('密码>>: ').strip()
                        if uname == 'egon' and pwd == '123':
                            print('登陆成功')
                            current_user['username']=uname
                            res=func(*args,**kwargs)
                            return res
                        else:
                            print('用户名或密码错误')
                    return wrapper

                def timmer(func):
                    def wrapper(*args,**kwargs):
                        start_time=time.time()
                        res=func(*args,**kwargs)
                        stop_time=time.time()
                        print(stop_time-start_time)
                        return res
                    return wrapper

                @timmer # timmer 统计的是auth+index的执行时间
                @auth
                def index():
                    time.sleep(1)
                    print('welcome to index page')
                    return 122

                index()













  • 相关阅读:
    405
    406
    4-1
    3-11
    3-10
    3-9
    3-8
    3-7
    3-5
    3-4
  • 原文地址:https://www.cnblogs.com/yangwei666/p/8670058.html
Copyright © 2020-2023  润新知