Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29390 > unrolled thread

Re: how to use property?

Started byChris Angelico <rosuav@gmail.com>
First post2012-09-18 08:12 +1000
Last post2012-09-18 08:12 +1000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: how to use property? Chris Angelico <rosuav@gmail.com> - 2012-09-18 08:12 +1000

#29390 — Re: how to use property?

FromChris Angelico <rosuav@gmail.com>
Date2012-09-18 08:12 +1000
SubjectRe: how to use property?
Message-ID<mailman.842.1347919948.27098.python-list@python.org>
On Tue, Sep 18, 2012 at 7:55 AM, Fernando Jiménez <the.merck@gmail.com> 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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web