[Python] Why do we need wrapper function in decorators?

Discussão em 'Python' iniciado por Stack, Junho 1, 2025.

  1. Stack

    Stack Membro Participativo

    If I create a decorator like the following:

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

    @my_decorator
    def just_some_fun():
    print("Wheee!")


    Another decorator can be defined as:

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

    @my_decorator
    def just_some_fun():
    print("some fun")


    What is the benefit of using "wrapper" function inside decorator? I didn't understand the purpose.

    Continue reading...

Compartilhe esta Página