为什么需要返回包装函数(python中的装饰器)

Why do wrapper functions need to be returned (decorators in python)

谁能解释为什么装饰器中的包装函数需要返回以及为什么

def decorate(func):
    def wrapper():
        print("Text")
        test_function()

@decorate
def test_function():
  print("More text")

test_function()

产生 NoneType object is not callable 而不是

def decorate(func):
    def wrapper():
        print("Text")
        test_function()
    return wrapper

@decorate
def test_function():
    print("More text")

test_function()

因为

@decorator
def f():
    ...

完全等同于

def f():
    ...
f = decorator(f)

所以 decorator 必须 return 一些东西才有意义,否则 f 将是 None.