Appropriate use of Property()

N

noemailplease0001

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, 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.

Thanks,
K
 
G

Gabriel Genellina

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, 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 usually implement read only attributes using properties. Sometimes I
may use getXXX() when it is slow to compute and I want to make
explicit that it is a function call. Just my opinion.
 
A

Asun Friere

The use of accessor methods is generally eschewed in python because it
is felt that reading and writing to foo.bar.baz, for instance, results
in cleaner (more legible) code than the idiom using
foo.getBar().getBaz()/foo.setBar().setBaz(). In the culture of some
other 00 languages, directly accessing an object's method is
considered a cardinal sin because it breaks encapsulation. The main
practical problem cited is something along this scenario:

You design an object to be used by others (objects or people), and
include an attribute x. Without an accesor method the others would
have to look directly at the attribute (ie via y = obj.x), which is
all fine and good until you decide to change the implementation of
your object, specifically you figure that x is really primary, but
should rather be computed from a and b, so you want to remove
attribute x and add a and b. Now with an accessor method getX(), all
you need do is change it to return a+b (or whatever), whereas if you
had relied on direct access, obviously all the other objects using
your obj.x will need to change. Therefore it is said that other
(objects and programmers) should be isolated from implentation details
via accessor methods.

Of course in python, should that situation arise, you simply employ
property() and from the point of view of those others nothing has
changed. In other words property() is a way of hiding implentation
details when you need to. So we can happily access object attributes
directly even if only apparently and enjoy clean code without guilt.
 
B

Bruno Desthuilliers

(e-mail address removed) 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')).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top