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


Groups > comp.lang.python > #29390

Re: how to use property?

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'debugging': 0.05; 'model,': 0.05; 'attributes': 0.07; 'python': 0.09; '@property': 0.09; 'guys!': 0.09; "object's": 0.09; 'part,': 0.09; 'sep': 0.09; 'underscore': 0.09; 'java,': 0.15; 'attributes,': 0.16; 'flow,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'java.': 0.16; 'wrote:': 0.17; 'instance,': 0.17; 'changes': 0.20; 'received:209.85.214.174': 0.21; 'posted': 0.22; 'example': 0.23; 'external': 0.24; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'c++': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'all.': 0.28; "i'm": 0.29; 'usually': 0.30; 'unlike': 0.30; 'code': 0.31; 'structure': 0.32; 'purposes,': 0.33; 'to:addr :python-list': 0.33; 'received:google.com': 0.34; 'subject:?': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'available.': 0.37; 'bad': 0.37; 'correctly': 0.37; 'quite': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'things': 0.38; 'nothing': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'most': 0.61; 'leading': 0.61; 'save': 0.61; 'touch': 0.69; 'fact,': 0.69; 'yourself': 0.77; 'bulk': 0.78; 'habit': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=+39F/6Py5PAL81KtdTTSO4Hbp63grLMhrb28isQsTGo=; b=HRT4hTUuHteIR+loRrJv0nG5C0rvTa/PXYdAzZ7k6czfpeyNsGNONQhitothw8uMbX 9A8MCtedG4v1FMCu7PSkIDe/+gVuu4OI7nm8+bsNBgWdESaJtRd6a11p3WCj+oj2f95b FBMGtBDOHKTbx0ejrn0zNSXNMiTyIsi347i5bN0TYY2G1hc4K3+MJj77cUQNJLbjzl1T p6aJCOWTAfdJeH1tclhEya4SBeDjE80DK+9AIKl2E8Hirxw1SVlw+5WApS5wBTMxDCh7 BvnjobVwrecZUGHGwEnI7TJzuOoWjQ4pk2L7dO//odSW9jQGdeLCsMW4hJn9bx0T2zSH 0dUg==
MIME-Version 1.0
In-Reply-To <CAGYVzb1-D4xyCeYHL6R1_O8msDfn9pd=UcuYhno5i3HCkB_t=A@mail.gmail.com>
References <CAGYVzb1-D4xyCeYHL6R1_O8msDfn9pd=UcuYhno5i3HCkB_t=A@mail.gmail.com>
Date Tue, 18 Sep 2012 08:12:26 +1000
Subject Re: how to use property?
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.842.1347919948.27098.python-list@python.org> (permalink)
Lines 29
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1347919948 news.xs4all.nl 6847 [2001:888:2000:d::a6]:47172
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29390

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web