Double陈

函数的装饰器

# 案例一错误改变了源代码
# import time
# def func(x,y):
# start=time.time()
# time.sleep(3)
# print("index %s %s" %(x,y))
# stop=time.time()
# print(stop-start)
#
# func(111,2222)

# 方案的优化(一)
# 将参数歇写活了能传入任何的参数
# import time
# def func(x,y,z):
# time.sleep(3)
# print("index %s %s %s" %(x,y,z))
#
# # 装饰器
# def come(*args,**kwargs):
# # start=time.time()
# # func(*args,**kwargs)
# # stop=time.time()
# # print(stop-start)
# #
# # come(111,222,333)
# # # 方案三的优化(二)
# # 对于装饰器不止应用于一个源代码的情况下 把任何被装饰对象都能用这个装饰器
# import time
# def func(x,y,z):
# time.sleep(3)
# print("index %s %s %s" %(x,y,z))
#
# # 装饰器
# def good(x):
# def come(*args,**kwargs):
# start=time.time()
# x(*args,**kwargs)
# stop=time.time()
# print(stop-start)
# return come
#
# f=good(func) #套用任意的被装饰对象
# #f就是come函数的内存地址
# f(111,222,3333) #为装饰对象传参数
# # 这里的f直接换func就可以实现装饰器了
#
#
# # 方案三的优化(三) 将come做的跟被装饰对象一模一样以假乱真 改动两个地方
#
# import time
# def func(x,y,z):
# time.sleep(3)
# print("index %s %s %s" %(x,y,z))
# return func
#
# # 装饰器
# def good(x):
# def come(*args,**kwargs):
# start=time.time()
# res=x(*args,**kwargs) #赋值给了res
# stop=time.time()
# print(stop-start)
# return res #得到了被装饰对象的返回值
# return come
#
#
#
#
# # 语法糖:在被装饰器正上方的单独一行写@装饰器的名字
# # 简化偷梁换柱的功能
# f=good(func) #套用任意的被装饰对象
# #f就是come函数的内存地址
# f(111,222,3333) #为装饰对象传参数
# # 这里的f直接换func就可以实现装饰器了
#
#
# import time
#
# # 装饰器
# def good(x):
# def come(*args,**kwargs):
# start=time.time()
# res=x(*args,**kwargs) #赋值给了res
# stop=time.time()
# print(stop-start)
# return res #得到了被装饰对象的返回值
# return come
#
# @good
# def func(x,y,z):
# time.sleep(3)
# print("index %s %s %s" %(x,y,z))
# return func
#



# 可运行最终的有参装饰器模型
# 装饰器
# import time
# def user_time(x):
# def good(*args,**kwargs):
# start=time.time()
# res=x(*args,**kwargs)
# stop=time.time()
# print(stop-start)
# return res #这里是被装饰对象的返回值
# return good
#
# @user_time
# def func(x,y):
# time.sleep(3)
# print("index %s %s" %(x,y))
# return func
#
# func(4,5)
#
#
# @user_time
# def home(u,w,d):
# time.sleep(8)
# print("%s %s %s"%(u,w,d) )
# return home
#
# home(9,8,10)
#



# 装饰器的叠加使用方法
# import time
#
# def timer1(func1):
# def good1():
# start1 = time.time()
# print("第一次")
# res1=func1()
# end = time.time()
# print("run time1 is %s" % (end - start1))
# return res1
# return good1
#
# def timer2(func2):
# def good2():
# start2 = time.time()
# print("第二次")
# res2=func2()
# end = time.time()
# print("run time2 is %s" % (end - start2))
# return res2
# return good2
#
#
#
# # 被装饰对象
# @timer1
# @timer2
# def index():
# time.sleep(3)
# print("from index")
# return 'ok'
#
#
# index()


# 易懂的多个装饰器运行结果
# 装饰器1
# def index1(x1):
# def a(*args,**kwargs):
# print("第一个装饰器开头")
# res1=x1(*args,**kwargs)
# print("第一个装饰器结尾")
# return res1
# return a
#
# #装饰器2
# def index2(x2):
# def b(*args,**kwargs):
# print("第二个装饰器开头")
# res2=x2(*args,**kwargs)
# print("第二个装饰器结尾")
# return res2
# return b
#
#
# # 被装饰对象
# @index1
# @index2
# def func(x, y):
# print("被装饰对象")
# return 111
#
# func(111,22222221)

 

posted on 2021-03-29 15:37  Double陈  阅读(28)  评论(0编辑  收藏  举报

导航