Decorators in Python

Hi All,

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:

  1. the greet function is wrapped by the do_twice decorator first
  2. 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?

  1. Calling wrapper_do_twice(‘Eva’) instead of Calling greet(‘Eva’) ?
  2. ‘wrapper_do_twice’ returned None instead of ‘greet’ returned None?

Appreciate if someone could enlighten me on this.

Cheers

Hey, nice advanced topic. Sooooo, decorators are SUPER confusing. There’s multiple layers of functions inside functions that you need to make them work. Now I think in your case it actually calls greet, not the wrapper, and that’s because your debug function is actually first in the chain. The chain of wrapper is actually the inverse IIRC, with the top one being … hmm I really can’t recall the order it’s that confusing.

So, put a debug print inside your debug wrapper_debug, have it print out what function it’s getting in func, and then put another in the do_twice. Probably before your call to @functools.wraps to see what is going on. That’s better than relying on my memory.