1、把一个函数名当做实参传给另外一个函数
作用:在不修改被装饰函数源代码的情况下为其添加功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
def bar():
time.sleep(
) print("in the bar")
def test1(func):
print(func)
start_time = time.time()
func() #run bar
stop_time = time.time()
print(
%(stop_time-start_time))
test1(bar)
#输出
>
in the bar
the func run the is
|
2、返回值中包括函数名
作用:不修改函数调用方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import time
def bar():
time.sleep(
) print("in the bar")
def test2(func):
print(func)
return func
#调用 test2 函数
bar = test2(bar)
bar()
#输出
>
in the bar
|