首页 > 编程语言 > 详细

python之装饰器

时间:2018-07-31 14:31:19      阅读:156      评论:0      收藏:0      [点我收藏+]

装饰器:  

技术分享图片
 1 import time
 2 def cal(l):
 3     start_time=time.time()
 4     res=0
 5     for i in l:
 6         time.sleep(0.1)
 7         res+=i
 8     stop_time = time.time()
 9     print(函数的运行时间是%s %(stop_time-start_time))
10     return res
11 
12 print(cal(range(100)))
View Code

 装饰器预演:

技术分享图片
 1 import time
 2 def timmer(func):
 3     def wrapper(*args,**kwargs):
 4         start_time=time.time()
 5         res=func(*args,**kwargs)
 6         stop_time = time.time()
 7         print(函数运行时间是%s %(stop_time-start_time))
 8         return res
 9     return wrapper
10 @timmer
11 def cal(l):
12     res=0
13     for i in l:
14         time.sleep(0.1)
15         res+=i
16     return res
17 
18 res=cal(range(10))
19 print(res)
View Code

 高阶函数:

  1.函数接收的参数是一个函数名
  2.函数的返回值是一个函数名
  3.满足上述条件任意一个,都可称之为高阶函数

  

python之装饰器

原文:https://www.cnblogs.com/sqy-yyr/p/9395454.html

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