Decorator projects#
Objectives#
In this section your knowledge and understanding of decorators will be stretched to the limit, as you’re asked to reimplement useful decorators available in the standard library.
Know that, for these exercises, you will need to read the documentation of the original decorator, play with it, experiment with it to see how it really works, and you might need to learn about other parts of the Python language that aren’t necessarily tied to decorators. The objective is for you to write your own implementation of the given decorator, and the task is for you to make your implementation as functionally faithful as possible…
Until you start to hit all sorts of super weird edge cases!
The true purpose of these exercises is to stretch your understanding of Python and to expose you to different tools from the standard library. When you get tired of working on a decorator because you’re now covering a weird edge case that only happens during full moons, if you’re running Python 3.12.2 in a blue toaster, stop.
The exercises that follow are not ordered by difficulty, as that is highly subjective and also depends on knowledge of other parts of the language that you may or may not have yet.
Pre-requisites#
For this section, it’s assumed you are comfortable with writing all sorts of decorators. It’s also assumed you are comfortable with being uncomfortable and struggling, since that’s what provides the best learning opportunities.
Solutions#
The decorators you’ll be writing are, most of them, written in Python themselves. This means you can open the source code of Python and look at the way the decorators are implemented!
If you get stuck, or if you get to a point where you’re happy with the work you’ve already done and you’d like to see what edge cases you’re missing, look up the decorator you’re implementing in the source code for Python. You will learn a lot.