python 装饰器

装饰器:本质就是一个函数
装饰器 = 高阶函数+嵌套函数

高阶函数:将一个函数作为另一个函数的参数。


import time

def aaa():
time.sleep(3)
print("aaa")

def test1(canshu1):
starttime = time.time()
canshu1()
endtime = time.time()
print('程序运行的时间是{}'.format(endtime-starttime))

test1(aaa)

这里就可以在不修改函数aaa源代码的情况下给函数aaa添加一个打印该函数运行时间的功能

 

posted @ 2018-07-09 15:31  日出东海,我心向西  阅读(78)  评论(0)    收藏  举报