What is a Decorator in Python?

Prem Vishnoi(cloudvala)
6 min readJust now

A decorator in Python is a design pattern that allows we to add new functionality to an existing object or function without modifying its structure.

It’s essentially a function that takes another function as an argument and returns a new function with added functionality.

We can think of it as a way to wrap additional logic around an existing function, such as logging, authentication, or caching, by using the @decorator_name syntax.

Image by pvishnoi

Decorators are usually implemented as functions (or classes) that take another function as an argument, extend its behavior, and return a new function with the extended behavior.

Why Do We Need Decorators?

by pvishnoi

Decorators are needed when we want to add functionality to an existing function in a reusable and clean way.

--

--

Prem Vishnoi(cloudvala)

Head of Data and ML experienced in designing, implementing, and managing large-scale data infrastructure. Skilled in ETL, data modeling, and cloud computing