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

    import time

    def timer(func):

          def deco(arg1):

                start_time=time.time()

                func(arg1)

                stop_time=time.time()

                print("the func run time is %s"%(stop_time-start_time))

            return deco

    @timer             

    def test1(name)

          print("test1",name)

    test1("zaizai")

    首先对于这个代码,@timer意味着test1=timer(test1),因为之前定义了timer(func)这个函数,而这个函数下面的逻辑是一个新的函数,deco,并且返回了deco的内存地址,所以这意味着timer(func),deco内存地址是一样的,然后有了test1=timer(test1)之后意味着,test1,timer(func),deco的内存地址是一样的,所以test1(),deco()是一样的,而test1这个函数是带参数的,所以,test1("zaizai")是可以运行的,他会运行deco("zaizai"),然后记录开始时间,运行func("zaizai"),然后print出来。

    假如两个arg1都没有,只运行test1(),这时候为什么运行不了?因为这时候其实是运行deco(),然后到func(),也就是运行test1(),而这时候没有参数,因此不能运行。

    如果参数不固定怎么办,将name,改成*args,**args即可

  • 相关阅读:
    Summarizing NUMA Scheduling两篇文章,解释得不错
    VCAP5-DCA – What’s new?
    NUMA总结。
    NUMA and vNUMA
    NUMA
    vsphere 5.1 性能最佳实践。
    vsphere性能
    mysql的事务,隔离级别和锁
    mysql5.7 生成列 generated column
    mysql8 公用表表达式CTE的使用
  • 原文地址:https://www.cnblogs.com/zaizaiaipython/p/7810913.html
Copyright © 2011-2022 走看看