Hacker News new | ask | show | jobs
by jcoq 1613 days ago
I would be terrified to see how some of the people I've worked with would abuse descriptors... thankfully they never knew about them(or it wasn't a python shop).

I've never understood the rationale that some developers employ for using `@property` (for purely stylistic) reasons and nobody has ever managed to give a very clear explanation of their criteria. Of course the concept is handy here and there, but some people make classes where every method is a property.

5 comments

It is just a way to migrate from "simple data access" to "complex data access involving some logic" without changing the API. This is indeed not very useful when you are the only person using your code, because you don't need to provide backwards compatibly guarantees on a source code level, but for library authors this is very much a necessity that saves library users a lot of unnecessary work updating their code, while allowing the library authors the freedom to change the implementation without changing the API.
I used @property a lot when I started with Python. I thought it was idiomatic and 'Pythonic', and I liked the idea of read-only properties, and I name private attrs with a leading underscore, but sometimes I wanted to expose the attr read-only without the underscore.

I rarely use @property now. It doesn't help clarity and isn't used in codebases I'm familiar with. Maybe overuse is a symptom of under-exposure and inexperience. Or maybe it's just personal tastes.

@property is useful when updating legacy code, but I agree, there’s not much calling for it. @cachedproperty has a lot more utility.
After getting a bit more used to the way python handles things I've come to realise that properties are basically only necessary if you need to do something special with the setter (even if just to absolutely forbid setting a value).
i wonder if the people who use `@property` for every method come from a language like ruby where providing getters/setters for every instance variable is a standard thing to do.