Python中Decorator的一些应用
import time
def display(func): #decorator装饰器
def wrapper(): #包装
t1 = time.time()
func()
t2 = time.time()
print(t2-t1)
return wrapper
def jg(x):
for i in range(2,x):
if x % i == 0:
return False
return True
@display #装饰器的语法糖
def prime():
for i in range(2,20):
if jg(i):
print(i)
prime()
具体的讲解
传送门
视频学习