装饰器是在不修改目标函数代码的前提下,为目标函数新增功能的函数
使用场景:为函数新增功能,解决重复性的操作,是的代码简洁,更模块化
性能测试,打印日志等
通过@调用装饰器
import time
def timeit(func):
def wrapper(*args):
start = time.time()
ret = func(*args)
print('user time is {:.4} s'.format(time.time() - start))
return ret
return wrapper
def is_prime(num):
if num < 2:
return False
elif num == 2:
return True
else:
for i in range(2,num):
if num % i == 0:
return False
return True
@timeit
def count_prime_nums(maxnum):
count = 0
for i in range(2,maxnum+1):
print('the num',i)
if is_prime(i):
print(i)
count += 1
return count
count = count_prime_nums(2)
print(count)
# print(9 % 3)
标签:count,prime,return,python,num,time,print,装饰
From: https://www.cnblogs.com/mktom/p/16865692.html