Computer Science
Grade 9
20 min
Decorators: Enhancing Functions with Style
Introduce decorators for adding functionality to functions without modifying their core code.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define a Python function that takes another function as an argument.
Explain the concept of a 'wrapper' function and its role in a decorator.
Write a simple decorator from scratch.
Apply a decorator to a function using the '@' syntax.
By the end of of this lesson, students will be able to create a decorator that works correctly with functions that accept arguments.
Identify and fix common errors in decorator implementation, such as forgetting to return the wrapper function.
What if you could give any of your functions a superpower, like a force field or a special announcement, without changing its original code? 🦸✨
In this lesson, you'll learn about Decorators, a powerful advanced Python feature. Decorators are like special �...
2
Key Concepts & Vocabulary
TermDefinitionExample
FunctionA reusable block of code that performs a specific task. You already know how to define and call functions using `def`.def greet(name):
print(f'Hello, {name}!')
First-Class FunctionsIn Python, functions are 'first-class citizens'. This means you can treat them just like any other variable, such as an integer or a string. You can pass them as arguments to other functions, and functions can return them.def say_hello():
return 'Hello!'
greeting = say_hello
print(greeting()) # This will print 'Hello!'
Higher-Order FunctionA function that either takes one or more functions as arguments, or returns a function as its result. Decorators are a type of higher-order function.def do_twice(func):
func()
func()
def say_whee(...
3
Core Syntax & Patterns
The Basic Decorator Pattern
def decorator_name(original_function):
def wrapper_function(*args, **kwargs):
# 1. Code to run BEFORE the original function
result = original_function(*args, **kwargs)
# 2. Code to run AFTER the original function
return result
return wrapper_function
This is the fundamental structure of any decorator. It defines an outer function (the decorator) and an inner function (the wrapper). The wrapper calls the original function and the decorator returns the wrapper.
Applying a Decorator
@decorator_name
def my_function():
# function code here
Place the '@' symbol followed by the decorator's name on the line directly above the function you want to enhance. This automatically applies the decorator's logic to your fu...
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
According to the 'Common Pitfalls' section, what must a decorator function always do at the end?
A.Call the original function.
B.Print a 'Done' message.
C.Import the `time` module.
D.Return the wrapper function.
Easy
In the `announce_task` example, what is the purpose of the `original_function()` line inside the `wrapper`?
A.To define the original function.
B.To execute the code of the function that was decorated.
C.To rename the original function to `wrapper`.
D.To check if the original function has any arguments.
Easy
What is the primary purpose of the '@' symbol when used directly above a function definition in Python?
A.To comment out the function
B.To apply a decorator to the function
C.To mark the function as private
D.To indicate the function returns a value
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free