装饰器中的包装器功能

时间:2020-06-07 22:13:39

标签: python function class decorator wrapper

我需要在这里具有包装器功能,但我真的不知道包装器功能在这里做什么,说实话,我试图在互联网上找到答案,但是我无法因此,如果您通过示例向我进行说明,我将不胜感激。

def decorate_it(func):
    def wrapper():
        print("Before function call")
        func()
        print("After function call")
    return wrapper

def hello():
    print("Hello world")

hello = decorate_it(hello)

hello()
# Prints Before function call
# Prints Hello world
# Prints After function call

我的意思是,当我在下面使用以下代码时,得到TypeError是什么问题:

def decorate_it(func):
    print("Before function call")
    func()
    print("After function call")

def hello():
    print("Hello world")

hello = decorate_it(hello)

hello()

0 个答案:

没有答案