装饰器

一:开放封闭原则,对扩展是开放的,对修改是封闭的
二:装饰器:本质是可以任意可调用的对象,被装饰的对象是任意可调用的对象。
装饰器的功能:在不修改被装饰对象源代码以及被装饰对象的调用方式的前提为其添加新新功能
原则:
  • 不能修改源代码
  • 不能更改调用方法
目标:添加新功能
装饰器语法:
在被装饰对象的正上方的单独一行,@装饰器
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()
posted @ 2017-06-14 15:54  皖心  阅读(137)  评论(0编辑  收藏  举报