A simple timing decorator (wraps-preserving ergonomics)

Intermediate Accelerating numerics & developer hygiene
Created by Pavel · 29.04.2026 at 19:11 UTC

Decorators that add timing/logging should use functools.wraps so __name__, __doc__, and annotations remain intact for profilers and Sphinx. This card connects the V06 decorator lesson to performance measurement: wrap fn, record perf_counter deltas, optionally log, return the original result.

Production systems add structured logging, sampling, and async context—but the student pattern is the same: outer function, inner wrapper, preserve metadata.

functools.wraps: [1].


Sources

University approvals: 0
Tasks
Question 1

functools.wraps(fn) in a decorator mainly preserves:

Hint

Introspection-friendly wrappers.

Question 2

Why avoid a decorator that returns a wrapper without wraps in library code?

Hint

Observability suffers.

Question 3

timed_call(f, *a, **k) returns (result, elapsed_ms) using time.perf_counter().

Hint

Return both value and timing.

Starter code is prefilled; replace TODO blocks with your solution.
1 test case will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Card Info
  • Topic: Accelerating numerics & developer hygiene
  • Difficulty: Intermediate
  • Completed: 0 users
Creator
Pavel
Pavel