Content pfp
Content
@
https://opensea.io/collection/dev-21
0 reply
0 recast
2 reactions

Boris pfp
Boris
@lokasan
Using Data and Non-Data Descriptors in Python: A Practical Perspective At first, Python descriptors seem like an advanced, behind-the-scenes feature. But once I started using them directly, especially data and non-data descriptors, they became essential for writing cleaner, more powerful code. Data descriptors implement both __get__ and __set__, and always take precedence over instance attributes. I used one to validate temperatures — even if someone tried obj.temp = "hot", the descriptor enforced correctness. This gave me full control over data integrity. Non-data descriptors, with only __get__, are overridden by instance attributes. That’s perfect for cases like @lazy_property, where I compute a value once and store it in the instance for future access — efficient and clean. The key benefit of descriptors is separation of behavior from logic. I no longer scatter validation or caching across the class — it all lives in small, reusable descriptor objects.
0 reply
0 recast
0 reaction