Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Implement a Basic Function DecoratorE02. Apply @syntax to Wrap a FunctionE03. Preserve Function Metadata with functools.wrapsE

Problem No 1

Implement a Basic Function Decorator

Easy

6 minute session

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

Code editor
Loading editor…

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.

09:17 AM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.