I am absolutly new in Python (I came from Java and C#)
I am stufying the decorator topic. So I have the following example:
# DECORATOR:
def my_decorator(func):
def wrap_func():
print('**************')
func()
print('**************')
return wrap_func
@my_decorator
def hello():
print('Hello World')
hello()
It seems to me that this is the logic (but I am not sure of this:
I am defining a my_decorator function taking another function as parameter. This my_decorator function wrap the function passed as parameter into a wrap_func() function that execute the function passed as parameter. In addition this wrap_func() function can perform some extra logic before and after the execution of the function passed as parameter (it is decorating the original function.
To say to the interpreter that a specific function have to use the decorator I have to use the syntax @decorator_name before the function definition.
So in my case: when I perform hello() call: Python know that the hello() function have to be decorated by my_decorator decorator so it is not directly executing the hello() function but it is performing the my_decorator() function passing hello() reference as parameter. So it can add extra logic before and after the hello() invoaction.
Is this reasoning correct or am I missing something??
I have another doubt: why the decorator function return the function that wrap our decoration logic?