[Python] when to use decorator and decorator factory?

Discussão em 'Python' iniciado por Stack, Agosto 24, 2025.

  1. Stack

    Stack Membro Participativo

    This is a bit confusing: a decorator takes no arguments, while a decorator factory takes arguments and still returns a decorator that decorates a function.

    A description of when to use each would be helpful.

    Edit: The confusion comes from the following example:

    def before_run(func):
    print "hello from before run"
    def handle_arg(a,b):
    if(a>0):
    a= 100
    return func(a,b)

    return handle_arg

    @before_run
    def running_func(a,b):
    print "a",a,"b", b
    return a+b


    Edit: Is there a way to do this using decorator factory by adding logging option(true or false)?

    Continue reading...

Compartilhe esta Página