It was bothering me for a long time. Why are decorator functions designed like that, they seem to me over complicated. Let's take for example something like this:
def dec(f):
def wrapper(a, b):
print('Hello')
f(a, b)
print('Bey')
return wrapper
@dec
def func(a, b):
print(a)
print(b)
Why do an additional function in the decorator, to wrap the functionality in the dec
function? I understand that the engine works like that, but why not make something simpler. Like this:
def dec(f, a, b):
print('Hello')
f(a, b)
print('Bey')
@dec
def func(a, b):
print(a)
print(b)
Change a little bit the engine and make the @ operator pass the name of the function and the parameters as parameters to the dec function. What are the benefits to use the first structure as we can easily decorate the second the same way?
If you can give me an example where the second example could not be possible to solve a problem - please share your knowledge.
The ticket was closed because some of the users have required more clarity to the problem, to help me solve the problem. I need to say that there is no problem to be solved. I have placed this question to collect information on why decorators are designed in the way they are. This will help me to understand what additional problems I can solve in the future with the tricks I can do with the existing model of the decorators.
Sorry if the question sounded unclear to you in any way. I thought it would be interesting to hear what experienced python users could say about this and share their knowledge with anyone who doesn't understand the purpose of a wrapper in a decorative function.