0

I've read all the answers to both these two questions: Why do we need wrapper function in decorators? and Why are decorator functions designed in the way they are?, but I still don't get it.

Why is a decorator written as such:

def decorator_function(function):
    def wrapper_function():
        print('Before the decoration')
        function()
        print('After the decoration')
    return wrapper_function

Rather than just like this:

def decorator_function(function):
    print('Before the decoration')
    function()
    print('After the decoration')
    return function

And why is is that when I do this:

@decorator_function
def original_function():
    print('Original function')

In the first case, with the wrapper function, if I call the function like this original_function() the return is:

Before the decoration
Original function
After the decoration

But in the second case, without the wrapper function, if I call the function, this is the result instead:

Before the decoration
Original function
After the decoration
Original function

The original function gets called one more time

Why do I need the wrapper function?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Laila Campos
  • 801
  • 1
  • 8
  • 21
  • 1
    Because the decorator returns a function object. You can decorate other things than functions. – tobias Jul 13 '21 at 21:22
  • @JohnColeman Really? Even if OP claims they read it and didn't get it? – Captain Trojan Jul 13 '21 at 21:24
  • 2
    Voting to close the question as a duplicate. The fact that you didn't understand the answer in the duplicate doesn't change its status as a duplicate. If you want to ask a new question, there needs to be something about the question which distinguished it from the other. – John Coleman Jul 13 '21 at 21:25
  • @CaptainTrojan I don't understand your issue. If OP wants to clarify what it is that they don't understand, then they could ask a new question, but duplicates are still duplicates. If you feel that duplicates shouldn't be closed in situations like this, perhaps you could raise the issue on [meta]. – John Coleman Jul 13 '21 at 21:28
  • 2
    Agreed. Stack Overflow requires *specific* questions. "I didn't understand this, can you explain it again?" isn't something that can be answered in Stack Overflow's format, because a) it isn't clear *why* OP doesn't understand and b) even if it were clear, it doesn't lead to *a question that can provide a good search engine result for others*. This is *not a discussion forum*. I would note there is *a* specific question here ("why does this code snippet have the behaviour that it does?"), but the question is answered by simply tracing through the logic of the code. – Karl Knechtel Jul 13 '21 at 21:30
  • OP: if you need help tracing through the logic, see the [long answer second from the top here](https://stackoverflow.com/questions/739654/how-to-make-function-decorators-and-chain-them-together), and take some time to study. – Karl Knechtel Jul 13 '21 at 21:34

0 Answers0