[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Appropriate use of Property

noemailplease0001

1/30/2008 1:18:00 AM

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
3 Answers

Gabriel Genellina

1/30/2008 6:04:00 AM

0

On 29 ene, 23:17, noemailplease0...@gmail.com wrote:

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

--
Gabriel Genellina

Asun Friere

1/30/2008 8:02:00 AM

0

On Jan 30, 5:03 pm, Gabriel Genellina <gagsl-...@yahoo.com.ar> wrote:
> On 29 ene, 23:17, noemailplease0...@gmail.com wrote:
>
> > 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.
>

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.

Bruno Desthuilliers

1/30/2008 9:16:00 AM

0

noemailplease0001@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')).