Decorators in Python

Python 10 min min read Updated: Mar 09, 2026 Advanced
Decorators in Python
Advanced Topic 3 of 10

Python Decorators

Decorators are an advanced and powerful feature in Python that allow developers to modify or extend the behavior of functions or methods without changing their actual code. They are widely used in frameworks, libraries, and large applications to add functionality such as logging, authentication, caching, and validation.

Decorators work by wrapping a function inside another function. This allows additional code to run before or after the original function executes.

What is a Decorator?

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

Python provides a special syntax using the @ symbol to apply decorators.

python def my_decorator(func): def wrapper(): print("Before function execution") func() print("After function execution") return wrapper @my_decorator def say_hello(): print("Hello!") say_hello()

In this example, the decorator adds extra behavior before and after the original function.

How Decorators Work

When a decorator is applied using the @decorator_name syntax, Python internally converts it into the following structure:

python say_hello = my_decorator(say_hello)

This means the original function is replaced by the decorated version.

Decorators with Arguments

Sometimes functions require parameters. Decorators can also handle such cases by accepting arguments in the wrapper function.

python def my_decorator(func): def wrapper(name): print("Welcome") func(name) print("Goodbye") return wrapper @my_decorator def greet(name): print("Hello", name) greet("Alice")

This decorator works with functions that accept parameters.

Using *args and **kwargs

To make decorators more flexible, developers often use *args and **kwargs so they can work with any function.

python def my_decorator(func): def wrapper(*args, **kwargs): print("Function is starting") result = func(*args, **kwargs) print("Function finished") return result return wrapper @my_decorator def add(a, b): return a + b print(add(5, 3))

This decorator can now handle any function with any number of arguments.

Built-in Decorators in Python

Python provides several built-in decorators that are commonly used in object-oriented programming.

  • @staticmethod
  • @classmethod
  • @property

@staticmethod Example

python class MathUtils: @staticmethod def add(a, b): return a + b print(MathUtils.add(3, 4))

@classmethod Example

python class Person: count = 0 def __init__(self): Person.count += 1 @classmethod def total_people(cls): return cls.count p1 = Person() p2 = Person() print(Person.total_people())

@property Example

python class Circle: def __init__(self, radius): self.radius = radius @property def area(self): return 3.14 * self.radius * self.radius c = Circle(5) print(c.area)

This allows accessing a method like an attribute.

Real-World Example

Decorators are widely used in web frameworks such as Flask and Django.

python def login_required(func): def wrapper(): print("Checking user authentication") func() return wrapper @login_required def dashboard(): print("Welcome to dashboard") dashboard()

This decorator ensures authentication before accessing a function.

Advantages of Using Decorators

  • Enhances code readability
  • Promotes code reuse
  • Separates concerns such as logging and validation
  • Reduces duplication of code

Best Practices for Decorators

  • Use decorators when adding reusable functionality.
  • Keep decorator logic simple.
  • Use *args and **kwargs for flexibility.
  • Document decorator behavior clearly.

Conclusion

Decorators are a powerful Python feature that allows developers to extend the behavior of functions and methods dynamically. By using decorators, you can add additional functionality such as logging, authentication, validation, and caching without modifying the original function.

Understanding decorators is an important step toward mastering advanced Python programming and building scalable applications.

In the next tutorial, we will explore Python Iterators and Generators and learn how Python handles efficient data processing.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators