Re: how to use property?

Discussion in 'Python' started by Chris Angelico, Sep 17, 2012.

  1. On Tue, Sep 18, 2012 at 7:55 AM, Fernando Jiménez <> wrote:
    > Hi guys!
    >
    > I'm noob in python and I would know how to correctly use the property. I
    > have read some things about it but I do not quite understand.
    >
    > But I think it's a bad habit to use _ to change the visibility of the
    > attributes as in JAVA.
    >
    > How to correctly use the property?


    The single leading underscore is nothing to do with visibility; it's a
    courteous request that external referents not touch something. In a
    "consenting adults" model, that's usually sufficient.

    For the most part, in fact, you don't need @property at all. Just make
    an object's members public and save yourself the trouble! Unlike the
    recommendation in C++ and Java, Python doesn't ask you to hide things
    and write code to make them available. Instead of starting with
    getters and setters, just start with a flat property, and move to
    getters/setters only when you find you need them.

    The example you posted is adding nothing to the work flow, but it's a
    good structure that you can tinker with. For instance, if you need to
    log all changes for debugging purposes, @property will make that easy.
    But for the bulk of attributes, it's complete overkill.

    ChrisA
     
    Chris Angelico, Sep 17, 2012
    #1
    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. Eric Newton
    Replies:
    3
    Views:
    9,601
    Brock Allen
    Apr 4, 2005
  2. Joshua Beall
    Replies:
    1
    Views:
    473
    Bertilo Wennergren
    Dec 10, 2003
  3. Noozer
    Replies:
    10
    Views:
    2,237
    Mitja
    Oct 13, 2004
  4. Nathan Sokalski
    Replies:
    1
    Views:
    1,136
    =?Utf-8?B?SmVk?=
    Apr 29, 2006
  5. Sky
    Replies:
    2
    Views:
    305
    Robert Koritnik
    Oct 1, 2004
Loading...

Share This Page