小编Ank*_*jan的帖子

为什么在装饰器中需要包装器功能?

如果我创建如下的装饰器:

def my_decorator(some_fun):
    def wrapper():
        print("before some_function() is called.")
        some_fun()
        print("after some_function() is called.")
    return wrapper

@my_decorator
def just_some_function():
    print("Wheee!")
Run Code Online (Sandbox Code Playgroud)

另一个装饰器可以定义为:

def my_decorator(some_fun):
    print("before some_function() is called.")
    some_fun()
    print("after some_function() is called.")

@my_decorator
def just_some_fun():
    print("some fun")
Run Code Online (Sandbox Code Playgroud)

两个装饰器将工作相同。在装饰器中使用“包装器”功能有什么好处。我没有达到目的。

python python-3.x python-decorators

8
推荐指数
2
解决办法
2612
查看次数

标签 统计

python ×1

python-3.x ×1

python-decorators ×1