It took me a Python/Django journey for ‘decorators’ to make sense.

A short story of how decorators as a programming concept made sense to me.

This is a part of the 100 Days To Offload challenge.


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.

The journey.

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
    # ...

I didn’t know what magical thing this @ symbol was and what it was doing. And what is require_http_methods?

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.

And…it clicked!

It makes sense! πŸŽ‰

I believe taking a step back from JavaScript actually helped me make sense of this…concept.

There was probably some sort of mental block that I was carrying with JavaScript that I am not with Python right now. And of course, the aforementioned article on RealPython is so lucid!

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. 😁


Comment via email.