首页 > 其他 > 详细

装饰器decorator

时间:2020-09-18 15:45:16      阅读:42      评论:0      收藏:0      [点我收藏+]
装饰器本质是函数,作用是为了其他函数添加附加功能
原则:不能改变其他函数的源代码和调用方式
包含知识内容:函数即变量,高阶函数,嵌套函数
技术分享图片
 1 import time
 2 
 3 
 4 def timer(func):
 5     def wraper(*args, **kwarge):  # 函数的嵌套:函数里面定义函数
 6         start_time = time.time()
 7         func(*args, **kwarge)
 8         stop_time = time.time()
 9         print(The func running time is %s % (stop_time - start_time))
10     return wraper
11 
12 
13 @timer
14 def test():
15     print(Begin to sleep..)
16     time.sleep(2)
17     print(in the test)
18     return 0
19 
20 
21 test()
View Code

 

装饰器decorator

原文:https://www.cnblogs.com/gzj137070928/p/13690675.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!