Checkpoint: Metaprogramming
advanced30 min readLesson 115 of 169
A class decorator that instruments every public method — the bridge between decorators and frameworks.
Checkpoint — instrument a class
Write @timed, a class decorator that wraps every public method
(not starting with _) of the decorated class so that each call's wall time
is appended to cls.__timings__[method_name] (a list, created on demand).
Requirements:
- method names stay correct on the wrapper (
__name__preserved) - private/dunder methods are untouched
- works for any class (the decorator must not hardcode names)
- timings must accumulate across instances
This is the exact pattern profilers, metrics libraries, and tracing hooks use when they cannot ask you to edit every method.