──────────────────────────────────────

59. Radio Shift Diagnostics · Add Behaviour with a Decorator

Write a wrapper that adds a trace and preserves the function’s result and name.

Lesson files · lessons/debug-and-measure/wrap-a-function-call

Before you start
- Store a function in a variable and call it later.
- Forward positional and keyword arguments with *args and **kwargs.

By the end
- Use @decorator to wrap a call and add a trace line.
- Forward the original arguments and return the original result, including zero.
- Use functools.wraps so the decorated function keeps its name.

Where you can use it
Add the same diagnostic message to several station planning functions while callers keep using the same arguments and results.

A decorator receives a function and returns a wrapper for it. Putting @show_call above def clip_label applies show_call(clip_label) when Python defines clip_label. The inner function remembers which original function it received. Later calls run the wrapper, which passes through the arguments and returns the result of the original call.

Put @wraps(function) above the inner function to keep details such as the original name and docstring. Use the outer return to hand back the wrapper itself. Use the inner return to hand back the result of the original call. Each has a different job.

Predict
What does the final print show?
- clip_label
- wrapped

Worked example (example.py)

from functools import wraps


def show_call(function):
    @wraps(function)
    def wrapped(*args, **kwargs):
        print(f"Checking {function.__name__}")
        return function(*args, **kwargs)

    return wrapped


@show_call
def clip_label(number, prefix="Clip"):
    return f"{prefix} {number}"


print(clip_label(3, prefix="Intro"))
print(clip_label.__name__)


Example result
Checking clip_label
Intro 3
clip_label

Complete show_call so the decorated block_minutes prints “Checking block_minutes” before doing its work. Forward the positional and keyword arguments, preserve the return value and keep the original function name with wraps. The input supplies clips and minutes per clip separated by spaces.

Starter program (starter.py)

from functools import wraps


def show_call(function):
    def wrapped(*args, **kwargs):
        print(f"Checking {function.__name__}")
        function(*args, **kwargs)

    return wrapped


@show_call
def block_minutes(clips, minutes_each=5):
    return clips * minutes_each


clips, minutes = (int(value) for value in input().split())
print(f"Minutes: {block_minutes(clips, minutes_each=minutes)}")
print(f"Function: {block_minutes.__name__}")


Starter input
3 4

Hint
Put @wraps(function) directly above def wrapped and use return function(*args, **kwargs) after the trace print.

Case 1. Try the sample
Input
3 4
Expected output
Checking block_minutes
Minutes: 12
Function: block_minutes

Case 2. No clips
Input
0 4
Expected output
Checking block_minutes
Minutes: 0
Function: block_minutes

Case 3. An explicit zero-minute override
Input
3 0
Expected output
Checking block_minutes
Minutes: 0
Function: block_minutes

Stretch
Give block_minutes a short docstring and inspect block_minutes.__doc__. Apply show_call to a second function and confirm that each decorated function keeps its own name and result.

Reflect
Which return hands back the function object, and which return hands back the result of a call?

Check your prediction
clip_label The name is read from the wrapper. The wraps decorator copied clip_label’s name onto it.
