[Tutor] @property vs @classmethod
mats at wichmann.us
Sat Jul 8 21:56:10 EDT 2017
On 07/08/2017 06:56 PM, Steven D'Aprano wrote:
> The pythonic way is not to use them at all.
> For experts only: you'll know when you need them.
> They really aren't even a little bit similar, if you thought they did
> that just means that you haven't understood what they do.
> property is for making computed atttributes.
> which people can then assign to, read or even delete. But occasionally
> you need to run some code whenever instance.x is accessed, and that's
> when you should use property.
When I use them (rarely), it's seemed to make sense when there's an
attribute which naturally has a relationship to another attribute and it
seems better to store one and compute one on demand rather than store
both as instance attributes and have to update both when either changes.
I'll admit that this has happened most often when writing a blog post
trying to explain properties :)
>From OO people, though, we get the sputtering But... But... what about
encapsulation, data hiding, etc?
More information about the Tutor