[Tutor] Properties of an object

spir denis.spir at free.fr
Fri Jan 30 13:02:23 CET 2009


Le Fri, 30 Jan 2009 12:22:10 +0100,
Vicent <vginer at gmail.com> a écrit :

> Thanks to all for your clear answers. I learned a lot about the way Python
> manages properties or attributes.
> 
> In my particular case, I think, as Spir said, that the best implementation
> depends on wether I am going to update the "base" properties very often or
> not, and wether I am going to access "the secondary properties" many times
> or not.
> 
> I didn't realized that, in case I define "firstpart" as a property, I have
> to update it every time the "list" property is updated/changed. Thank you
> for that obvius detail!

Do not forget that, as someone said, if you let firstpart and lastpart available as "data attributes" (rather avoid 'properties'), then the client code is implicitely allowed to set/change it. Which would obviously lead to inconsistent status. So that it may be better, if only for clarity, to write them as methods, even if there are automatically updated each time primary data are changed. Or else, use the "_prop" convention to remove ambiguity. The actual semantics are "information attribute", that is read-only, a distinction which is not native to python: you have to maintain it.
Finally, you could use "properties", in the sense of python, as suggested in a previous message: if you simply define a 'getter" without any 'setter', the data won't be 'settable' from outside. (It may be nevertheless better to define a "setter" that raises an error with a sensible message...)

Denis

------
la vida e estranya


More information about the Tutor mailing list