Appropriate use of Property()

Discussion in 'Python' started by noemailplease0001@gmail.com, Jan 30, 2008.

  1. Guest

    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
     
    , Jan 30, 2008
    #1
    1. Advertising

  2. On 29 ene, 23:17, 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
     
    Gabriel Genellina, Jan 30, 2008
    #2
    1. Advertising

  3. Asun Friere Guest

    On Jan 30, 5:03 pm, Gabriel Genellina <> wrote:
    > On 29 ene, 23:17, 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.
     
    Asun Friere, Jan 30, 2008
    #3
  4. 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')).
     
    Bruno Desthuilliers, Jan 30, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?a2Fub25lcw==?=

    This header must be modified with the appropriate property

    =?Utf-8?B?a2Fub25lcw==?=, Dec 3, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    717
    =?Utf-8?B?a2Fub25lcw==?=
    Dec 3, 2004
  2. darrel
    Replies:
    6
    Views:
    381
    darrel
    Jan 20, 2005
  3. Michael Wilcox

    Forms an appropriate use of tables?

    Michael Wilcox, Sep 6, 2003, in forum: HTML
    Replies:
    7
    Views:
    528
    EightNineThree
    Sep 6, 2003
  4. SenthilVel
    Replies:
    1
    Views:
    3,542
    dd_fruend
    Aug 4, 2006
  5. Chris Mohan
    Replies:
    0
    Views:
    107
    Chris Mohan
    Jul 21, 2004
Loading...

Share This Page