Re:Attrs – the Python library everyone needs (2016)
by lambdadmitry on Saturday December 25, 2021 @05:04AM
(#29681940)
Note that that "location" property should be a method instead of property to signal that it does something potentially complex and slow. Making it a property practically guarantees that someone will use it in a loop without much second thought, and that's how you get N+1.
Re:Attrs – the Python library everyone needs (2016)
by joshuamorton on Saturday December 25, 2021 @12:25PM
(#29684978)
Fair point! one of various @cached_property decorators might fix this, depending on the precise use case, but yeah this is an important consideration when defining your API.
These are useful even if only due to the "I can take the three related pieces of information I have and stick them next to each other". That is, if I have some object I'm modelling and it has more than a single attribute (a user with a name and age, or an event with a timestamp and message and optional error code), I have a nice way to model them.
Then, the important thing is that these are still classes, so you can start with
and have that evolve over time to and since it's still just a class, it'll still work. It absolutely makes modelling the more complex cases easier too.