0

I am new to Python decorators.I am able to run the decorator without passing any arguments. And is working fine.

I am interested in passing to_decorator value to the decorator function as show below.

Is this possible?

 def test(self, *args):
    return "from test"


def decorate(function):
    def wrap_function(*args, **kwargs):
        print(*args)
        return function(*args, **kwargs)

    return wrap_function


class Sample():
    @decorate
    def message(*args, **kwargs):
        to_decorator = test(*args)
        print(to_decorator)


Sample.message("Hello123")

Thanks in advance.

  • 1
    Try to run that code and you will see if that's possible. Anyhow, your question is unclear. Firstly, your code is badly indented, which is a no-go in Python. Then, it references `to_decorator` before defining it, which doesn't work. However, it's also unclear what you want to achieve with that. Can you write code that is functionally equivalent to what you want to achieve without decorators? – Ulrich Eckhardt Mar 30 '18 at 08:45
  • I think an example input/output would be great here. What you're asking for (passing an argument to a decorator) is possible, but your example use above is odd. `to_decorator` won't be initialised at this point, and it's unclear what you're expecting this to do. – Robert Seaman Mar 30 '18 at 08:46
  • I would like to pass the output of test function to the decorator function to_decorator. Is it possible to do so. – Ravi Kiran Gururaja Mar 30 '18 at 08:55

0 Answers0