概念:是一个闭包,把一个函数作为参数然后返回一个替代版的函数,本质上是一个返回函数的函数
作用:在不修改原函数的前提下增加函数的功能,最好使用装饰器
def say():
print("zutuanxue_com is a good man")
def newSay():
print("**************")
say()
newSay()
装饰器实现不修改原函数而增加新的功能
# 参数f:要给哪个函数增加功能,那么就在调用wrapper()函数时传递那个函数
def wrapper(f):
# inner()函数即为替代版函数,要实现原函数的功能,并增加新功能
def inner():
# 在原函数基础上增加新的功能
print("*************")
# 实现原函数功能
res = f()
return res
return inner
def say():
print("zutuanxue_com is a good man")
# newSay = wrapper(say)
# newSay()
say = wrapper(say)
say()
def wrapper(f):
# 原函数的参数在inner函数这里传递
def inner(name, age):
# 增加的判断age的功能
if age < 0:
age = 0
res = f(name, age)
return res
return inner
def say(name, age):
return "%s is a good man!He is %d years old!"%(name, age)
say = wrapper(say)
# 增加判断年龄如果小于0,则打印0即可
print(say("zutuanxue_com", -18))
def wrapper1(f):
def inner(name, age):
if age < 0:
age = 0
res = f(name, age)
return res
return inner
def wrapper2(f):
def inner(name, age, height):
if age < 0:
age = 0
res = f(name, age, height)
return res
return inner
def wrapper3(f):
def inner(name, age, *args, **kwargs):
if age < 0:
age = 0
res = f(name, age, *args, **kwargs)
return res
return inner
def say1(name, age):
return "%s is a good man!He is %d years old!"%(name, age)
def say2(name, age, height):
return "%s is a good man!He is %d years old!His height is %.2f"%(name, age, height)
#装饰函数
say1 = wrapper3(say1)
say2 = wrapper3(say2)
print(say1("zutuanxue_com", -19))
print(say2("kaige", -20, 173.55))
#给函数增加打印一串星号的功能,通用所有函数
def wrapper(f):
def inner(*args, **kwargs):
print("*************")
res = f(*args, **kwargs)
return res
return inner
python2.4开始支持使用@符号将装饰器应用到函数上,只需要在函数定义时加上"@装饰器名称"即可完成装饰操作
def wrapper(f):
def inner():
print("*************")
res = f()
return res
return inner
@wrapper #相当于 say = wrapper(say)
def say():
print("zutuanxue_com is a good man")
say()
规定函数执行次数
# 接收装饰器传递的参数
def wrapper(count=3):
# 接收带添加功能的函数
def deco(f):
# 接收原函数的参数
def inner(*args, **kwargs):
for i in range(count):
f(*args, **kwargs)
return inner
return deco
# 没有参数传递,也需要带着小括号
@wrapper()
def say(name, age):
print("%s is a good man!He is %d years old!"%(name, age))
say("zutuanxue_com", 18)
def wrapper1(f):
def inner1(*args, **kwargs):
print("enter inner1")
res = f(*args, **kwargs)
print("exit inner1")
return res
return inner1
def wrapper2(f):
def inner2(*args, **kwargs):
print("enter inner2")
res = f(*args, **kwargs)
print("exit inner2")
return res
return inner2
def wrapper3(f):
def inner3(*args, **kwargs):
print("enter inner3")
res = f(*args, **kwargs)
print("exit inner3")
return res
return inner3
'''
装饰时:从距离近的装饰器开始装饰
执行时:从距离远的装饰器内部函数开始执行
'''
@wrapper1
@wrapper2
@wrapper3
def say():
print("zutuanxue_com is a good man")
'''
say = wrapper3(say)
say = wrapper2(say)
say = wrapper1(say)
---------------------------
inner3 = wrapper3(say)
say = inner3
inner2 = wrapper2(inner3)
say = inner2
inner1 = wrapper1(inner2)
say = inner1
'''
print("-------------------------")
say()
以上的装饰器均由函数实现的,也可以使用类来实现装饰器,后面讲
def wrapper(f):
count = 1
def inner(*args, **kwargs):
nonlocal count
print("第%d次执行"%(count))
res = f(*args, **kwargs)
count += 1
return res
return inner
@wrapper
def say():
print("zutuanxue_com is a good man")
say()
say()
say()
import time
def wrapper(f):
def inner(*args, **kwargs):
t1 = time.time() #获取当前时间的时间戳
res = f()
t2 = time.time()
print("耗时%.2f秒"%(t2-t1))
return res
return inner
@wrapper
def say():
print("zutuanxue_com is a good man")
# 休息2秒钟,可以是浮点数
time.sleep(2)
print("zutuanxue_com is a nice man")
say()
import random
import time
def retry(count=3, wait=0, exceptions=(Exception,)):
def deco(f):
def inner(*args, **kwargs):
for i in range(count):
try:
res = f(*args, **kwargs)
return res
except exceptions as e:
if not (i == count - 1):
time.sleep(wait)
return -1 #这里用的表示失败的值一定不能出现在成功中
return inner
return deco
@retry()
def func():
num = random.choice([1,2,3,4])
if num <= 2:
num / 0
else:
print("********************", num)
if func() == -1:
print("程序运行出错")