Computer Science
Grade 9
20 min
Decorators with Arguments: Customizing Decorator Behavior
Learn to create decorators that accept arguments for customized behavior.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define a decorator that accepts custom arguments.
Explain the three-level nested function structure required for decorators with arguments.
Apply a decorator with arguments to a function using the '@' syntax.
Pass arguments from the decorator to the inner wrapper function.
Differentiate between the structure of a simple decorator and a decorator that takes arguments.
Create a practical decorator that modifies a function's behavior based on a given argument, such as repeating its execution.
Imagine you could give a magic spell a special instruction, like 'cast this 3 times' or 'only cast for a king'. What if you could do the same for your Python functions? 🧙✨
This tutorial will teach you how to create powerful, flexibl...
2
Key Concepts & Vocabulary
TermDefinitionExample
Decorator with ArgumentsA special type of decorator that you can customize by passing arguments to it, just like you would with a regular function.@repeat(3) is a decorator with an argument. It tells the decorated function to run 3 times.
Decorator FactoryThe outermost function in a decorator with arguments. Its job is to accept the arguments and 'manufacture' and return the actual decorator.In `@repeat(3)`, the `repeat` function is the decorator factory. It takes `3` as an argument.
Three-Level StructureThe nested function pattern required for decorators with arguments: an outer factory function, a middle decorator function, and an inner wrapper function.def factory(arg): -> def decorator(func): -> def wrapper(*args, **kwargs):
Outer Function (Factor...
3
Core Syntax & Patterns
The Three-Level Nested Function Pattern
def decorator_factory(arg1, arg2, ...):
def decorator(func):
def wrapper(*args, **kwargs):
# 1. Code to run BEFORE the original function
# (You can use arg1, arg2 here)
result = func(*args, **kwargs)
# 2. Code to run AFTER the original function
return result
return wrapper
return decorator
This is the fundamental structure. The factory takes arguments and returns the decorator. The decorator takes the function and returns the wrapper. The wrapper executes the new logic and the original function.
Applying the Decorator with Arguments
@decorator_factory(argument1, argument2)
def my_function():
pass
When using the '@' syntax, you call the...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Easy
In the structure of a decorator with arguments, what is the primary role of the outermost function, often called the 'decorator factory'?
A.To execute the original function's code.
B.To accept arguments for the decorator and return the actual decorator function.
C.To replace the original function with new logic.
D.To directly receive the function being decorated.
Easy
In the context of decorators with arguments, what is the primary role of the outermost function, often called the 'decorator factory'?
A.To execute the original function multiple times.
B.To directly wrap the original function and modify its behavior.
C.To accept arguments for the decorator and return the actual decorator function.
D.To hold the return value of the original function.
Easy
According to the tutorial's `repeat(n)` example, what is the correct syntax to apply the decorator to a function named `show_message` so it runs 5 times?
A.@repeat(5)
B.@repeat 5
C.@repeat(n=5)
D.@repeat
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free