Python Academy · Lesson

Decorators

Learn how to use decorators to modify or extend functions.

Lesson 1 of 511 steps

Decorators is a free Python Academy lesson on CoddyKit. This is lesson 1 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, and your progress syncs across the web and the CoddyKit app. The Python Academy course includes 5 lessons in total.

Introduction to Decorators

Decorators are a powerful feature in Python that allow you to modify the behavior of functions or methods. They are often used to add functionality to functions without modifying their code.

In this lesson, you’ll learn what decorators are, how to use them, and how to create your own decorators.

Decorators — illustration 1

What Is a Decorator?

A decorator is a function that takes another function as input and returns a new function with added functionality. It’s often used with the @decorator_name syntax.

# Basic example of a decorator
def decorator(func):
    def wrapper():
        print("Before the function call")
        func()
        print("After the function call")
    return wrapper

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

say_hello()

How Decorators Work

When you use a decorator, Python replaces the original function with the wrapper function returned by the decorator.

# How decorators work
def decorator(func):
    def wrapper():
        print("Wrapper called")
        func()
    return wrapper

@decorator
def greet():
    print("Greetings!")

print(greet.__name__)  # Outputs: wrapper
greet()

Using Decorators with Arguments

Decorators can work with functions that take arguments by using *args and **kwargs in the wrapper function:

# Decorator for functions with arguments
def decorator(func):
    def wrapper(*args, **kwargs):
        print(f"Arguments: {args}, {kwargs}")
        return func(*args, **kwargs)
    return wrapper

@decorator
def add(a, b):
    return a + b

print(add(3, 5))

Chaining Multiple Decorators

You can use multiple decorators on a single function. They are applied in the order they appear, from top to bottom:

# Chaining multiple decorators
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 greet():
    print("Hello!")

greet()

Creating Parameterized Decorators

Parameterized decorators take arguments and allow for more flexible functionality:

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

@repeat(3)
def say_hello():
    print("Hello!")

say_hello()

Common Use Cases for Decorators

Decorators are commonly used for:

  • Logging function calls and their arguments.
  • Measuring execution time of functions.
  • Access control (e.g., checking user permissions).
  • Caching results for performance improvements.

Built-in Decorators in Python

Python provides some built-in decorators, including:

  • @staticmethod: Defines a static method.
  • @classmethod: Defines a class method.
  • @property: Defines a property method.

Common Mistakes with Decorators

Here are some mistakes to avoid:

  • Not preserving the original function’s name and docstring (use functools.wraps).
  • Using decorators unnecessarily for simple tasks.
  • Forgetting to handle arguments in the wrapper function.

What Did We Learn?

In this lesson, you learned:

  • What decorators are and how they work in Python.
  • How to create and use basic, parameterized, and chained decorators.
  • Common use cases and built-in decorators in Python.

Great job! Let’s move to the next topic.

Decorators — illustration 11
Free to start

Learn Python with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
76
Lessons
320

Frequently Asked Questions

Is the “Decorators” lesson free?

Yes — the full text of “Decorators” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO. The Python Academy course includes 5 lessons in total.

What will I learn in “Decorators”?

Learn how to use decorators to modify or extend functions. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 5.

How long does the “Decorators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Decorators
  2. Generators
  3. Context Managers (with Statements)
  4. Comprehensions (List, Set, and Dictionary)
  5. Iterators and Iterables
← Back to Python Academy