Lesson guide
What this Python exercise practices
Implement a Basic Function Decorator is a beginner practice lesson that focuses on functions, parameters, return values. It is designed to be solved in about 6 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- Function parameters
- Return values
Difficulty and time
- Level
- Beginner
- Estimated time
- 6 minutes
Related public exercises
Summary
Create a simple decorator that modifies the return value of a function.
Problem statement
Implement a decorator named double_output. When applied to a function that returns an int or float, the decorator should return twice that numeric result. Use functools.wraps to preserve the wrapped function's __name__ and __doc__. If the wrapped function returns a non-numeric value, raise a TypeError with a clear message. Your decorator should work with positional and keyword arguments and should not modify the original function's behavior beyond doubling the return value.
Task
Write a decorator double_output that doubles the numeric result returned by the wrapped function while preserving its metadata.
Examples
Basic integer doubling
Input
add(2, 3)
Output
10
Explanation
add returns 5, the decorator doubles it to 10.
Input format
A call expression of a decorated function, for example: add(2, 3)
Output format
The doubled numeric return value of the decorated function (as a number).
Constraints
- The decorator must use functools.wraps. - The decorated function will return int or float in valid inputs. - If the wrapped function returns a non-numeric value, raise TypeError. - Support arbitrary positional and keyword arguments.
Samples
Sample input 0
add(1, 4)
Sample output 0
10
Explanation 0
1 + 4 = 5 -> doubled to 10
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.