I first heard of decorators when trying to pick up TypeScript through a side-project: Celestial, an IndieWeb publishing interface. (Unfortunately, I’m no longer developing it.)
It just felt like this big, confusing, mysterious thing.
My learning process usually involves making sure I fully understand the concept I’m about to make use of, before actually using it. This means using the following decorator in Django made me uncomfortable:
from django.views.decorators.http import require_http_methods @require_http_methods(["GET", "POST"]) def my_view(request): # I can assume now that only GET or POST requests make it this far # ... pass
I didn’t know what magical thing this
@ symbol was and what it was doing. And what is
Here’s where I did something unusual: I stuck with the uncomfortable feeling; resigning to the fact that even though I don’t understand what it is or how it works… that it does something I want it to do: limit by verb which HTTP requests can come in. And that’s alright for now.
Then, days later, I circled back by reading an article on RealPython about decorators. I had to make sense of it! I had used it after all.
It makes sense! 🎉
Now that I understand it, it seems odd to think how it wasn’t clear to me all along. That’s how programming is, I guess. Sometimes, concepts just click in a way you don’t expect them to!
Onwards with Python, then. 😁
This is Day 021 in my 100 Days To Offload.