装饰器

一.装饰器的作用

  在不改变原来函数的函数体内的代码和函数的调用基础上,给一个函数添加新的功能

二.函数即变量

·  变量名就相当于一个门牌号,变量的内容会在内存中划分一块空间给它。函数名也是同样的作用。

  当一个函数定义好了,python的解释器读到def ***():会理解为内存中有这样一个门牌号里面装的是函数。但此时不会解析函数体的代码。执行时再转回去解释。

def test():
    print("in the test")
    bar()
def bar():
    print("int the bar")
test()
def test():
    print("in the test")
    bar()
test()
def bar():
    print("int the bar")

注意观察

三.高阶函数

  定义函数时,将一个函数名作为形参,或返回一个函数名 

四.嵌套函数

        函数定义内部再定义一个函数

五.装饰器

     高阶函数加嵌套函数

import   time
def wap_timer(thou)
    def  timer(func):
         def deco(*args,**keyargs):
              start_time = time.time()
              res =  func()
              stop_time = time.time()
              return res
         return deco
    return timer
  
@wap_timer(thou=local)
def test(n):
      time.sleep(3)
      print(n)
     return n
test(3)
原文地址:https://www.cnblogs.com/gjx1212/p/11565950.html