装饰器中的包装函数(Python)

问题描述 投票:0回答:1

我需要在这里具有包装器功能,但我真的不知道包装器功能在这里做什么,说实话,我试图在互联网上找到答案,但是我不能,所以我想如果您通过示例向我解释它,则非常感谢。

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()
python function class decorator wrapper
1个回答
0
投票
hello = decorate_it(hello) *

hello # this exists as a "variable", even though it's a function call*

我希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.