装饰器的作用 —— 不想修改函数的调用方式,但是还想在原来的函数前后添加功能
原则 :开放封闭原则
开放 : 对扩展是开放的
封闭 : 对修改是封闭的
装饰器的本质 : 闭包函数
装饰器用法
装饰不带参数的函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import time def timmer(func): def inner(): start_timev= time.time() data = func() end_time = time.time() print(end_time-start_timev) return data return inner @timmer def func(): time.sleep(0.5) print("不带参数的函数,等会我要装饰下自己") return '我是函数' ret = func() print(ret)View Code
装饰带参数的函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def timmer(func): def inner(*args, **kwargs): start_timev= time.time() data = func(*args, **kwargs) ##参数灵活设置,不固定 end_time = time.time() print(end_time-start_timev) return data return inner @timmer def func(a, b): time.time(0.5) print(f'我是参数{a}, 我是另一个参数{b}') return a+b ret = func('hello', 'girls') print(ret)View Code
装饰器带参数,可根据场景动态实现函数是否需要装饰
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#带参数的装饰器 FLAG = False ###通过配置表设置函数是否需要被装饰 def timmer(flag): def wrapper(func): def inner(*args, **kwargs): if flag: start_timev = time.time() data = func(*args, **kwargs) end_time = time.time() print('函数执行时间差', end_time - start_timev) return data else: data = func(*args, **kwargs) return data return inner return wrapper @timmer(FLAG) def func1(a, b): time.sleep(0.5) print(f'我是参数{a}, 我是另一个参数{b}') return a+b @timmer(FLAG) def func2(l): time.sleep(0.5) print(type(l), f'我是大列表') return '我是列表我是老大' func1('hello', 'girls') func2([1,3,5,'a', 's', 8, 'k'])View Code
多个装饰器装饰同一个函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def wrapper1(func): def inner1(): print('wrapper1 ,before func') ret = func() print('wrapper1 ,after func') return ret return inner1 def wrapper2(func): def inner2(): print('wrapper2 ,before func') ret = func() print('wrapper2 ,after func') return ret return inner2 def wrapper3(func): def inner3(): print('wrapper3 ,before func') ret = func() print('wrapper3 ,after func') return ret return inner3 @wrapper3 @wrapper2 @wrapper1 def f(): print('in f') return '哈哈哈' print(f())View Code
functools.wraps:保留原函数信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from functools import wraps def wrapper(func): #func = holiday @wraps(func) def inner(*args,**kwargs): print('在被装饰的函数执行之前做的事') ret = func(*args,**kwargs) print('在被装饰的函数执行之后做的事') return ret return inner @wrapper #holiday = wrapper(holiday) def holiday(day): '''这是一个放假通知''' print('全体放假%s天'%day) return '好开心' print(holiday.__name__) print(holiday.__doc__) ##函数注释 ret = holiday(3) #inner print(ret)View Code
demo1:用户登录才能加车或删除商品
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
FLAG = False def login(func): def inner(*args, **kwargs): global FLAG if FLAG: return func(*args, **kwargs) username = input('请输入用户名: ') pwd = input('请输入密码: ') if username=='myname' and pwd == '123': FLAG = True return func(*args, **kwargs) else: print('login fail') return inner @login def shoplist_add(): print('增加一件物品') @login def shoplist_del(): print('删除一件物品') shoplist_add() shoplist_del()View Code
demo2:为调用函数加上记录调用功能,要求每次调用函数都将被调用的函数名称写入文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def log(func): def inner(*args,**kwargs): with open('log','a',encoding='utf-8') as f: f.write(func.__name__+'\n') ret = func(*args,**kwargs) return ret return inner @log def shoplist_add(): print('增加一件物品') @log def shoplist_del(): print('删除一件物品')View Code
标签:return,Python,ret,func,time,print,装饰,def From: https://www.cnblogs.com/margret/p/17337744.html