Master Python Function Decorator Tutorial: A Step-by-Step Guide

Logan Jul 13, 2026

Python function decorators are a powerful tool to enhance your code's readability and maintainability. They allow you to wrap another function in order to extend its behavior, without permanently modifying it. In this tutorial, we will explore the concept of Python function decorators, their syntax, and how to create and use them.

Python Decorators Tutorial: Examples, Use Cases & Best Practices
Python Decorators Tutorial: Examples, Use Cases & Best Practices

Before diving into decorators, let's ensure you have a basic understanding of higher-order functions and closures. Python allows functions to accept other functions as arguments and even return functions as outputs. These are known as higher-order functions, and they form the foundation of decorators.

| Pythonista Planet
| Pythonista Planet

What are Function Decorators?

In simplest terms, a function decorator is a function that takes in a function and returns another function. The returned function is said to be decorated. Decorators provide a way to modify or enhance the behavior of other functions or methods in Python.

an image of a computer screen with the text, 1 basic decorator on it
an image of a computer screen with the text, 1 basic decorator on it

Decorators follow the syntax: @. They are placed just above the definition of the function they are decorating. This is a compact way to apply decorators and makes your code more readable.

Simple Decorator Example

How Python Decorators Work: 7 Things You Must Know
How Python Decorators Work: 7 Things You Must Know

A simple decorator might change the behavior of a function based on some condition. Here's an example that adds a timestamp to each function call:

  def timestamp_decorator(func):
    def wrapper():
      import datetime
      print(f"Timestamp: {datetime.datetime.now()}")
      func()
    return wrapper

  @timestamp_decorator
  def say_hi():
    print("Hi there!")

  say_hi()

This will output the timestamp followed by "Hi there!".

Python Decorators - The advanced concept in functional programming
Python Decorators - The advanced concept in functional programming

Decorating Functions with Arguments

Decorators can also handle functions with arguments. Let's modify our timestamp decorator to include the function name:

  def timestamp_decorator(func):
    def wrapper(*args, **kwargs):
      import datetime
      print(f"{func.__name__} was called at {datetime.datetime.now()}")
      return func(*args, **kwargs)
    return wrapper

  @timestamp_decorator
  def add_numbers(a, b):
    return a + b

The Simplest Tutorial for Python Decorator | Towards Data Science
The Simplest Tutorial for Python Decorator | Towards Data Science

Now, our decorator works with functions having arguments.

Decorators with Arguments

Python Decorators with Examples
Python Decorators with Examples
Python Decorators Explained – Functions, Syntax, and Real Examples
Python Decorators Explained – Functions, Syntax, and Real Examples
Python OOP Tutorial 6: Property Decorators - Getters, Setters, and Deleters
Python OOP Tutorial 6: Property Decorators - Getters, Setters, and Deleters
Python Tutorial
Python Tutorial
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
Master Python Functions – Write Smarter Code! 🐍✨
Master Python Functions – Write Smarter Code! 🐍✨
9 Python Projects for Beginners (Build Your Portfolio Fast)
9 Python Projects for Beginners (Build Your Portfolio Fast)
the top 50 python project ideas
the top 50 python project ideas
Python + Art = 🌹 Try This Fun Code!
Python + Art = 🌹 Try This Fun Code!
Machine Learning with Python Tutorial
Machine Learning with Python Tutorial
How to Use the String join() Method in Python
How to Use the String join() Method in Python
Dunder or Magic Methods in Python
Dunder or Magic Methods in Python
a person typing on a laptop with the words 10 python project ideas for beginners
a person typing on a laptop with the words 10 python project ideas for beginners
draw a stunning tree with Python!
draw a stunning tree with Python!
How to Built a Desktop App with Python | Full Walkthrough
How to Built a Desktop App with Python | Full Walkthrough
30 Python Projects For Beginners and Advanced Learners (2023)
30 Python Projects For Beginners and Advanced Learners (2023)
💜 Python Trick: Use f-Strings 🐍 Write cleaner and more readable Python code with f-strings ✨
💜 Python Trick: Use f-Strings 🐍 Write cleaner and more readable Python code with f-strings ✨
two black and gold posters with different types of wires on them, one in the middle
two black and gold posters with different types of wires on them, one in the middle
Secure Python Packages Built From Source | ActiveState
Secure Python Packages Built From Source | ActiveState

Sometimes, you might want to pass arguments to your decorators. You can do this by defining your decorator to accept an additional argument:

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

  @repeat(3)
  def say_hi():
    print("Hi there!")

This will print "Hi there!" three times.

Built-in Function Decorators

Python has several built-in decorators, such as @property, @staticmethod, and @classmethod. We won't go into detail about them here, but understanding their use can greatly enhance your coding skills.

property: Allows you to define getter, setter, and deleter methods for an instance variable. @property is used to define a getter method.

staticmethod: Indicates that the method does not change the state of the object (instance) it is called on. @staticmethod is used to define a method that belongs to the class rather than an instance.

classmethod: Similar to staticmethod, but it can access the class itself (via cls). @classmethod is used to define a method that belongs to the class rather than an instance.

Now that you understand the basics of Python function decorators, you can use them to create cleaner, more readable, and more maintainable code. Keep practicing and exploring more advanced topics like decorator composition and "decorators on top of decorators". Happy coding!