• 装饰器


    一:开放封闭原则,对扩展是开放的,对修改是封闭的
    二:装饰器:本质是可以任意可调用的对象,被装饰的对象是任意可调用的对象。
    装饰器的功能:在不修改被装饰对象源代码以及被装饰对象的调用方式的前提为其添加新新功能
    原则:
    • 不能修改源代码
    • 不能更改调用方法
    目标:添加新功能
    装饰器语法:
    在被装饰对象的正上方的单独一行,@装饰器
    eg.:@timmer==index=timmer(index)
     import time
     import random
    #装饰器
     def timmer(func):
         # func=index
         def wrapper():
             start_time = time.time()
             func() #index()
             stop_time=time.time()
             print('run time is %s' %(stop_time-start_time))
         return wrapper
    #被装饰函数
     def index():
         time.sleep(random.randrange(1,5))
         print('welecome to index page')
    
     def home():
         time.sleep(random.randrange(1,3))
         print('welecome to HOME page')
    
     index=timmer(index) #index=wrapper
     home=timmer(home)
    
     index() #wrapper()
     home()
    #装饰器修订
    import time
    import random
    #装饰器
    def timmer(func):
        def wrapper(*args,**kwargs):
            start_time = time.time()
            res=func(*args,**kwargs)
            stop_time=time.time()
            print('run time is %s' %(stop_time-start_time))
            return res
        return wrapper
    #被装饰函数
    
    @timmer
    def index():
        time.sleep(random.randrange(1,5))
        print('welecome to index page')
    @timmer
    def home(name):
        time.sleep(random.randrange(1,3))
        print('welecome to %s HOME page' %name)
        return 123123123123123123123123123123123123123123
    
    
    index()
    import time
    import random
    from functools import wraps #导入wrap模块
    def timmer(func):
        @wraps(func)#引用该模块使装饰器的备注信息换为被装饰函数的备注信息
        def wrapper():
            '''hello mona'''
            start_time=time.time()
            res=func()
            end_time=time.time()
            print('run time is %s'%(end_time-start_time))
            return res
        return wrapper
    
    @timmer
    def usr_in():
        '''usr in function'''
        time.sleep(random.randrange(1,4))
        print('welcome to mona homepage')
    
    print(usr_in.__doc__)

     有参装饰器

    db_path=r'/Users/mona/Desktop/python学习/study/homework/day7/a.txt'
    dic_login={'name':None,'status':None}
    def deco(auth_type):
        def auth(func):
            def wrapper():
                if auth_type=='file':
                    if dic_login['name'] and dic_login['status']:
                        res = func()
                        return res
                    else:
                        usr = input('please input your name:')
                        pw =int(input('please input your password:'))
                        f = open(db_path,'r',encoding='utf-8')
                        for line in f:
                            line = eval(line)
                            if usr == line['name'] and pw == line['pw']:
                                dic_login['name']=usr
                                dic_login['status']=True
                                print('login successful')
                                res=func()
                                return res
                        f.close()
                elif auth_type=='mysql':
                    print('mysql认证方式')
                else:
                    print('不知道')
            return wrapper
        return auth
    
    @deco('file')
    def add1():
        print('test1')
    @deco('mysql')
    def add2():
    print('test2') @deco('aaa') def add3(): print('test3') add1() add2()
  • 相关阅读:
    teamviewer被识别为商用的解决办法
    PLY调试笔记——待解决的问题
    python中的反射
    类的继承顺序
    面向对象---继承
    类成名和命名空间
    面向对象编程
    模块的初始
    装饰器
    列表推导式,生成器
  • 原文地址:https://www.cnblogs.com/mona524/p/7009300.html
Copyright © 2020-2023  润新知