Decorator Function

Last Updated: 02th September 2025


A decorator in Python is a function that modifies or enhances another function without changing its actual code. It uses the @decorator_name syntax.

Hinglish Tip 🗣: Decorator ek function hota hai jo dusre function ko input ke roop me leta hai, uske behavior ko modify/add karta hai bina uske actual code ko change kiye.

✏️ Basic Decorator

def greet_decorator(func):
    def wrapper():
        print("👋 Hello ,This is Tukka learn")
        func()
        print("✅ That’s the end of today’s session")
    return wrapper

@greet_decorator   # applying decorator
def my_class():
    print("📊 We are learning Python Functions")

# Call
my_class()

Decorator With Arguments

def log_decorator(func):
    def wrapper(name):
        print(f"🔍 Logging: Calling function with argument: {name}")
        func(name)
    return wrapper

@log_decorator
def say_hello(student):
    print(f"Hello {student}, keep practicing Data Science!")

say_hello("Ravi")

Decorator With Keyword Arguments

def decoratorFun(anyFun):
    def wrapper(*args,**kwargs):
        print("Welcome")
        return anyFun(*args,**kwargs)
    return wrapper

@decoratorFun
def hello(name):
    print(f"Hello {name}")

print(hello("Ravi"))