I am currently on the “Learning to Speak Object Oriented” chapter of the book.
While doing some further study on this topic on the Internet, I came across “Decorator”.
Based on some of the articles on the blogs on the Internet, I get the idea that a decorator is basically a function which modifies the functionality of an input function that is being passed into it.
However, I do have some problems understanding one of the example that involves multiple decorators as shown below (refer to the attachment image):
I understand that with multiple decorators, the sequence that it is evaluated, for instance, in this case, would be as follow:
- the greet function is wrapped by the do_twice decorator first
- the resulting wrapped function is then wrapped again by the debug decorator.
What confuses me here is that, isn’t that the 1st evaluation (do_twice(greet)) will result in the wrapper_do_twice function being returned, which should then be the function being passed as an argument into the debug decorator instead of greet?
And if that’s the case, shouldn’t the output (as circled in red in the attachment image) be printed out as follow?
- Calling wrapper_do_twice(‘Eva’) instead of Calling greet(‘Eva’) ?
- ‘wrapper_do_twice’ returned None instead of ‘greet’ returned None?
Appreciate if someone could enlighten me on this.