Python Decorators, What are they?

DataGeeks
4 min readDec 22, 2023

Python decorators are a powerful and elegant feature that allows developers to modify or extend the behaviour of functions or methods. In this blog post, we’ll explore the concept of decorators and delve into a real-world use case to illustrate their application.

Understanding Python Decorators:

In Python, decorators are functions that wrap around other functions, enhancing or modifying their behaviour. They provide a clean and concise way to implement cross-cutting concerns such as logging, authentication, and performance monitoring.

Decorator Examples and Use cases:

Let’s start with a simple example of a decorator:

def my_decorator(func):
def wrapper():
print("Hey this is decorator starting point")
func()
print("Hey this is decorator ending point")
return wrapper

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

---------Output----------

Hey this is decorator starting point
Hello!
Hey this is decorator ending point

In this example, my_decorator is a function that takes another function (func) as its parameter and returns a new function (wrapper). The wrapper the function adds some behaviour before and after calling the original function. The @my_decorator syntax is a…

--

--

DataGeeks

A data couple, Having 15 years of combined experience in data and love to share the knowledge about Data