What are Decorators ?
                   A decorator in Python is a function that allows us to modify or enhance another function’s behavior without changing its original code.

A decorator:

  • takes a function as input
  • wraps additional behavior around it
  • returns a modified function

A Simple Decorator :

Python
def my_decorator(func): def wrapper(): print("Before function execution") func() print("After function execution") return wrapper def say_hello(): print("Hello Python") decorated = my_decorator(say_hello) decorated() #output Before function execution Hello Python After function execution 


using decorator syntax :

Python
def my_decorator(func): def wrapper(): print("Before execution") func() print("After execution") return wrapper @my_decorator def show(): print("Inside function") show() #output Before execution Inside function After execution

Note : The @my_decorator line automatically wraps the function and applies extra functionality


*args and **kwargs : 

Python
def logger(func): def wrapper(*args, **kwargs): print("Arguments:", args, kwargs) return func(*args, **kwargs) return wrapper @logger def add(a, b): return a + b print(add(5, 3)) #output Arguments: (5, 3) {} 8