python

Decorator

Define the decorator: The decorator function wraps another function (func) with extra behavior before and after calling it.

def my_decorator(func):
    def wrapper():
        print("Before the function runs")
        func()
        print("After the function runs")
    return wrapper

@my_decorator
def say_hello():
    print("Hello!")

say_hello()
Before the function runs
Hello!
After the function runs