python 装饰器 decorator

import time
def  deco(fun):
    def  wrapper():
        t1=time.time()
        res=fun()
        t2=time.time()
        print("total time is: {:.4} s".format(t2-t1))
        return res
    return wrapper 

def is_prime(num):
    if(num==1):
        return 0
    elif(num==2):
        return 0
    else:
        for i in range(2,num):
            if(num%i==0):
                return 0
        return 1
@deco  
def   count_nums():
    count=0
    for i in range(2,1001):
        if(is_prime(i)):
           count+=1
    return count


count=count_nums()
print(count)
           

坑      明天在补

参考https://www.zhihu.com/question/26930016

posted @ 2019-04-25 22:23  流照君  阅读(137)  评论(0编辑  收藏  举报