Decorators
Learn how to use decorators to modify or extend functions.
Decorators is a free Learn AI with Python lesson on CoddyKit — 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 Learn AI with Python learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
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.

2
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()3
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()4
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))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()6
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()7
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.
8
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.
9
10
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.
11
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.

Frequently asked questions
Is the “Decorators” lesson free?
Yes — the full text of “Decorators” is free to read here on the web, and the Learn AI with Python course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Decorators”?
Learn how to use decorators to modify or extend functions. You practise Learn AI with Python 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 Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 5, so you can start here or from the beginning and move at your own pace.
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 Learn AI with Python lesson?
Yes. Every Learn AI with Python 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.