python学习-装饰器

来源:互联网 发布:php大牛的博客 编辑:程序博客网 时间:2024/06/16 14:33
def outer(func):    def inner():        print("begin")        func()    return inner@outerdef f1():    print("hello")f1()--------------运行结果-------------beginhello

“@outer”功能:

  • 自动执行outer函数,并将下面的函数名称f1当做参数传递给outer函数
    • 执行f1时,实际上会自动去执行outer函数,并且此时的参数func即f1
  • 将outer函数的返回值,重新赋值给f1
    • outer函数的返回值为inner(为函数,非inner(),inner()对应的是执行print与f1(),对应的返回值为None),因此f1被赋值为inner。f1()即对应inner()。
0 0