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

python开发【函数】:装饰器的诞生

时间:2018-08-12 00:31:36      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:返回   print   运行时间   返回值   怎么   bar   *args   imp   pytho   

计算函数的运行时间

import time
def bar():
    print("in the bar")
    time.sleep(2)
def foo(func):
    start_time=time.time()
    func()
    end_time=time.time()
    print("in the foo")
    print("该函数运行的时间是%s"%(end_time-start_time))
foo(bar)

虽然已经实现功能,但是改变了函数bar的调用方式,优化后如下:

import time
def bar():
    print("in the bar")
    time.sleep(2)
def foo(func):
    start_time=time.time()
    print("in the foo")
    return func
    end_time=time.time()
    print("该函数运行的时间是%s"%(end_time-start_time))
bar=foo(bar)
bar()
-------------输出------------------
in the foo
in the bar

总结:函数bar的调用方式未改变,但是与题目要求又不一致,函数遇到return后结束了,未输出运行函数的时间,继续优化:

import time
def foo(func):
    def inner():
        start_time=time.time()
        print("in the foo")
        func()
        end_time=time.time()
        print("该函数运行的时间是%s"%(end_time-start_time))
    return inner
@foo#bar=foo(bar)=inner
def bar():
    print("in the bar")
    time.sleep(2)
bar()
-------------输出------------------
in the foo
in the bar
该函数运行的时间是2.0122246742248535

总结:此时装饰器的雏形已形成,那如果函数带参数的话,会是怎么样了,继续搞。

import time
def foo(func):
    print("in the foo")
    def inner(*args,**kwargs):
        start_time=time.time()
        print("in the inner")
        func(*args,**kwargs)
        end_time=time.time()
        print("该函数运行的时间是%s"%(end_time-start_time))
    return inner
@foo #bar=foo(bar)=inner
def bar(x,y):
    a=x+y
    print("in the bar")
    print("两数相加和为%s"%a)
    time.sleep(2)
bar(3,5)
-------------输出------------------
in the foo #无论函数是否运行,都会默认执行这一步
in the inner
in the bar
两数相加和为8
该函数运行的时间是2.0000240802764893

总结:无论函数是否执行,都会默认先执行foo函数的,就是这么给力,那如果函数还有返回值了,怎么弄了

import time
def foo(func):
    print("in the foo")
    def inner(*args,**kwargs):
        start_time=time.time()
        print("in the inner")
        res=func(*args,**kwargs)
        print("函数的返回值为:%s"%res)
        end_time=time.time()
        print("该函数运行的时间是%s"%(end_time-start_time))
    return inner
@foo #bar=foo(bar)=inner
def bar(x,y):
    a=x+y
    print("in the bar")
    time.sleep(2)
    return a
bar(3,5)
-------------输出------------------
in the foo
in the inner
in the bar
函数的返回值为:8
该函数运行的时间是2.000295639038086

总结:只是稍加变动就搞定了,那如果装饰器也带参数,实现只有在特定情况下计算函数运行的时间,否则不执行

import time
def foo(auth_type):
    print("in the foo")
    def outer(func):
        print("in the outer")
        def inner(*args,**kwargs):
            if auth_type=="run":
                start_time=time.time()
                print("in the inner")
                res=func(*args,**kwargs)
                print("函数的返回值为:%s"%res)
                end_time=time.time()
                print("该函数运行的时间是%s"%(end_time-start_time))
            else:
                print("输入的auth_type不是run")
        return inner
    return outer
@foo("run") #1、outer=foo("run"),2、bar=outer(bar)=inner
def bar(x,y):
    a=x+y
    print("in the bar")
    time.sleep(2)
    return a
bar(3,5)
-------------输出------------------
in the foo#只要加了装饰器,会默认输出
in the outer#只要加了装饰器,会默认输出
in the inner
in the bar
函数的返回值为:8
该函数运行的时间是2.0101609230041504

总结:完美搞定,加了装饰器的效果后,会首先运行foo("run")得到outer,然后把函数bar当作参数传入给outer得到outer(bar)运行后得到inner。

python开发【函数】:装饰器的诞生

标签:返回   print   运行时间   返回值   怎么   bar   *args   imp   pytho   

原文地址:https://www.cnblogs.com/yan-long/p/9461661.html

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