Python学习第四十一天函数装饰器传参数的用法

   在不改变函数的结构的基础,我们给函数加新的功能,用是函数装饰器,如果要给函数传递参数,那么应该怎么做呢

@timer
def test2(name,age):
  time.sleep(3)
  print('name: %s age: %s' % (name, age))

test2('dongye',33)

def timer(func): #是test2函数,高阶函数
  def deco(*args,**kwargs): 
    start_time=time.time()
    func(*args,**kwargs)
    stop_time = time.time()
    print('the func run time is %s' % (stop_time-start_time))
  return deco #返回deco函数的内存地址

文章来自 http://www.96net.com.cn

猜你喜欢

转载自www.cnblogs.com/96net/p/9698920.html