1. 首页
  2. 编程面试题
  3. Python
  4. Python基础

对装饰器的理解,你能写出一个计时器装饰器,它能记录函数的执行时间吗?



装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象。


import time
    def timeit(func):
        def wrapper():
            start = time.clock()
            func()
            end = time.clock()
            print('used:',end-start)
            return wrapper
    @timeit
    def foo():
        print('in foo()'foo())

发布者:admin,如若转载,请注明出处:https://ai1024.vip/27492.html

QR code
//