techmore.in

Python - Decorators

What Are Decorators?

Decorators are a powerful feature in Python that allow you to modify or extend the behavior of functions or classes without changing their actual code. They are often used for logging, access control, memoization, and more.

Function Decorators

A decorator is a function that takes another function as an argument and returns a new function.

python
def greet(func):
    def wrapper():
        print("Hello!")
        func()
        print("Goodbye!")
    return wrapper

@decorate
def say_name():
    print("My name is Python.")

say_name()

Why Use Decorators?

  • To add functionality without modifying the original function.
  • To enforce DRY (Don’t Repeat Yourself) principles.
  • To implement reusable behaviors like logging or timing.

How Decorators Work

The @decorator_name syntax is a shorthand for:

python
say_name = greet(say_name)

Decorators with Arguments

You can make a decorator accept arguments by defining another nested function.

python
def repeat(n):
    def decorator(func):
        def wrapper(*args, **kwargs):
            for _ in range(n):
                func(*args, **kwargs)
        return wrapper
    return decorator

@repeat(3)
def greet():
    print("Hi!")

greet()

Chaining Multiple Decorators

Multiple decorators can be stacked on a single function, executing from bottom to top.

python
def decorator1(func):
    def wrapper():
        print("Decorator 1")
        func()
    return wrapper

def decorator2(func):
    def wrapper():
        print("Decorator 2")
        func()
    return wrapper

@decorator1
@decorator2
def say_hello():
    print("Hello!")

say_hello()

Class Decorators

You can also create decorators that modify or enhance classes.

python
def add_repr(cls):
    cls.__repr__ = lambda self: f"{self.__class__.__name__}({self.__dict__})"
    return cls

@add_repr
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

p = Person("Alice", 25)
print(p)

When to Use?

  • To enhance function behavior (like logging or security checks).
  • To enforce constraints or validation on inputs.
  • To implement AOP-style (aspect-oriented programming) code.

Pros and Cons

  • Pros: Clean syntax, reusable behavior, separation of concerns.
  • Cons: May reduce readability if overused, requires understanding closures and higher-order functions.