Decorator is a powerful in object oriented programming language since it allows programmers to modify the behavior of the function or class.
In decorators functions are taken as the arguments into another functions and then called inside the wrapper function
Syntax for decorator
@doc_decorator
def hello_decorator():
print("hello world")
In the above code @doc_decorator is a callable function, will add some code on the top of some another callable functions, hello_decorator function and return the wrapper function.
Printing documentation using decorator :
def my_doc(any_function):
def test():
return f'{any_function.__doc__}'
return test
@my_doc
def user():
'''hello user what's up'''
return 'user'
Here we create a function my_doc and passing the any_function as a parameter, inside the my_doc we create another function(test) to work with the documentation part, i e any function from the outside of my_doc function can be implemented in the new function (test).
we need to call my_doc function as @my_doc before staring a function to include the my_doc function.