With that out of the way, we can talk about decorators. Decorators wrap a function in another function that modifies the original in some way, such as adding functionality, modifying arguments or results, and so on. Decorators are identified by the @foo
nomenclature on the line above a function/method definition.
The workhorse of a decorator function is defining the wrapper
function within it. In this case, the wrapper
function is a nested function that actually does the modification work, though the decorator name is what is called.
def fun_decorator(some_funct): def wrapper(): print("Here is the decorator, doing its thing") for i in range(10): print(i) print("The decorator is done, returning to the originally scheduled function") print(some_funct()) return wrapper
- Define the main function...