码迷,mamicode.com
首页 > 编程语言 > 详细

Python_装饰器进阶_32

时间:2019-04-07 21:41:44      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:out   用户   turn   装饰器   多个   执行   lse   fun   pytho   

#带参数的装饰器
#500个函数
import time
FLAGE = True
def timmer_out(flag):
    def timmer(func):
        def inner(*args,**kwargs):
            if flag:
                start = time.time()
                ret = func(*args,**kwargs)
                end = time.time()
                print(end-start)
                return ret
            else:
                ret = func(*args, **kwargs)
                return ret
        return inner
    return timmer
# timmer = timmer_out(FLAGE)    #timmer = timmer_out(FLAGE)
#wahaha = timmer(wahaha)        #@timmer
@timmer_out(FLAGE)    
def wahaha():
    time.sleep(0.1)
    print(wahahahahahaha)

@timmer_out(FLAGE)
def erguotou():
    time.sleep(0.1)
    print(erguotoutoutou)

wahaha()
erguotou()

 

 

#多个装饰器装饰一个函数
def wrapper1(func):
    def inner1():
        print(wrapper1 ,before func)
        ret = func()
        print(wrapper1 ,after func)
        return ret
    return inner1

def wrapper2(func):
    def inner2():
        print(wrapper2 ,before func)
        ret = func()
        print(wrapper2 ,after func)
        return ret
    return inner2

def wrapper3(func):
    def inner3():
        print(wrapper3 ,before func)
        ret = func()
        print(wrapper3 ,after func)
        return ret
    return inner3

@wrapper3
@wrapper2
@wrapper1
def f():
    print(in f)
    return 哈哈哈

print(f())

#记录用户的登录情况
#计算这个函数的执行时间

 

Python_装饰器进阶_32

标签:out   用户   turn   装饰器   多个   执行   lse   fun   pytho   

原文地址:https://www.cnblogs.com/LXL616/p/10667182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!