zoukankan      html  css  js  c++  java
  • 装饰器

    import time
    def foo():
    print("this is foo")
    time.sleep(2)
    # foo()

    def bar():
    print("this is bar")
    time.sleep(3)
    # bar()

    def show_time(f):
    def inner():
    starttime = time.time()
    f()
    endtime = time.time()
    print("执行时间:%s"%(endtime - starttime))
    return inner

    # show_time(foo)# 这里虽然计算了时间,但是调用的函数方法发生改变,
    # show_time(bar) #之前的是foo,bar,现在是show_time

    #给show_time变量赋值
    foo = show_time(foo) #这个执行后会执行show_time函数
    #这里需要思考下,怎么样在执行foo() 时能再次执行show_time
    #这里就需要添加内部函数inner ,把inner的对象地址返回给show_time
    #再次执行foo()函数时就是执行的inner(这样就没有改变原本的函数条用方法)
    foo()

    bar = show_time(bar)

    bar()
    --------------------------------------------------------------------------------------------------------------
    import time
    def show_time(f):
    def inner():
    starttime = time.time()
    f()
    endtime = time.time()
    print("执行时间:%s"%(endtime - starttime))
    return inner
    @show_time #foo = show_time(foo) @show_time同等与foo = show_time(foo)
    def foo():
    print("this is foo")
    time.sleep(2)
    #foo = show_time(foo)
    foo()
    @show_time
    def bar():
    print("this is bar")
    time.sleep(3)
    #bar= show_time(bar)
    bar()
  • 相关阅读:
    获取app下载链接
    查找文件的路径
    回忆基础:制作plist文件
    Ping++中的AlipaySDK和AlicloudUTDID冲突解决方案
    CocoaPods常用操作命令
    自签名配置HTTPS
    Instruments10 分析某个类中方法的执行时间
    iOS KVC/KVO
    iOS 系统架构及常用框架
    LINQ to SQLite完美解决方案
  • 原文地址:https://www.cnblogs.com/TKOPython/p/11734174.html
Copyright © 2011-2022 走看看