What are Python decorators?

A Python decorator is a specific change that we make in Python syntax to alter functions easily. Decorators were added in Python 2.4 to make function and method wrapping (a function that receives a function and returns an enhanced one) easier to read and understand.

Function decorators: adjust the behavior of a preexisting function by hooking into its start-up and teardown mechanisms.


import functools

def my_decorator(myfunc):
    def function_myfunc():
        print("In the decorator!")
        print("After the decorator!")
    return function_myfunc 

def my_function():
    print("I am the function!")