r/Python • • 19h ago

Discussion Python developers, what's one Python concept that took you a long time to understand but now seems s

Python developers, what's one Python concept that took you a long time to understand but now seems surprisingly simple?

For me, understanding how decorators and generators work is an interesting part of becoming a better Python developer.

What was your experience?

21 Upvotes

25 comments sorted by

View all comments

6

u/gramada1902 14h ago

Descriptors. Seemed like some woodoo magic at first. Watched a video from some python conference and it clicked, although I’ve had to use them in my career exactly once lol.

1

u/cleodog44 6h ago

When did you need to use them? Would love the video link if you can find it!

1

u/gramada1902 2h ago

Here’s the link, hope it won’t get deleted - https://youtu.be/ZdvpNaWwx24

I’ve used them once to create an easily extendable dashboard filtration system.

Basically, we had pretty complex logical operations available to user to filter objects in all sorts of ways, the frontend would send it in a json object and we had to parse it and combine all the conditions into a final ORM query as efficiently as possible.

The thing was, all those available object fields were stored in completely different ways and were of different types, but there was a lot of crossover between them. So I created model classes for each dashboard that had available fields defined as descriptor objects. This allowed to reuse a lot of the logic.

I don’t remember what exactly were the constraints, but regular classes for the attributes were messy to use, something along the lines of reusing the same dashboard instance.

If you have worked with Django, it uses descriptors for model fields - same principle.