Appropriate use of Property()

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Jan 30 04:15:55 EST 2008


noemailplease0001 at gmail.com a écrit :
> Property() can be used to rid ourselves of the extra effort of using
> two different methods (getAttrib() setAttrib()) for access of an
> attribute without giving direct access to the attribute,

NB : properties are for computed attributes, not to "avoid giving direct 
acces to (an) attribute". If what you need is really a public attribute, 
  then use a plain attribute - knowing you'll be able to switch to a 
property if and when the need arises.

> thus making
> it more elegant. So, the outsider using my module accesses the
> attribute with the syntax 'Object.attrib', but since this syntax looks
> as if he is accessing the attribute (rather than through a
> descrtiptor), should we subscribe to this syntax only when the
> attribute is both readable and writable? i.e.,
> if I have an attribute which I strongly believe would always be only
> readable, should I go with the old style 'Object.getAttrib()'?
> Would like to know different perspectives.

I just can second Gabriel on this: as long as it's not computation 
intensive, I use a read-only attribute (ie: fset and fdel raising 
AttributeError('attribute XXX is read-only')).



More information about the Python-list mailing list