Lesson 51 of 55
15 mins readPython Decorators and Metadata Reflection
In Plain English
Decorators provide a declarative way to instrument, log, modify, or observe class definitions and their methods, widely used in modern frameworks.
Core Rules to Remember

Class Decorators: Wraps constructors to add metadata, dependency injection, or telemetry.

Method Decorators: Instruments method execution with logging, authorization, and caching.
Live Interactive Example
Hit Run Code to see it liveApplying Method Instrumentation Decorator Pattern
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Output Console
Click "Run Code" to view the rendered output.
How it works: Decorators wrap class methods or constructors to provide cross-cutting concerns.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Implement a Simple Timing Wrapper
Write function withTiming<T>(fn: () => T, label: string): T that prints `[Start: ${label}]`, executes fn(), and returns the result.
Run with fn returning 100 * 2 with label "compute" and print `Returned: ${result}`.
1
2
3
4
5
6
7
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.