1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| def outer(func):
def inner():
print('开始延迟')
func()
print('延迟结束')
return inner
def yanchi():
import random
import time
seconds = random.randint(1,5)
print(f"延迟{seconds}秒")
time.sleep(seconds)
'''
fn = outer(yanchi)
fn()
'''
# 糖写法
@outer # 定义在目标函数之上,和第14行功能一致
def yanchi_2():
import random
import time
seconds = random.randint(1,5)
print(f"延迟{seconds}秒")
time.sleep(seconds)
yanchi_2()
|