Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #29394
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'attribute': 0.05; 'model,': 0.05; 'attributes': 0.07; 'read-only': 0.07; 'try:': 0.07; 'python': 0.09; '@property': 0.09; 'attribute.': 0.09; 'compute': 0.09; 'guys!': 0.09; "object's": 0.09; 'part,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sep': 0.09; 'subclass': 0.09; 'terry': 0.09; 'underscore': 0.09; 'def': 0.10; 'java,': 0.15; 'attribute,': 0.16; 'attributes.': 0.16; 'examples:': 0.16; 'java.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'define': 0.20; 'error.': 0.21; 'constant': 0.22; 'needed.': 0.23; 'external': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'c++': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'all.': 0.28; 'chris': 0.28; 'class': 0.29; "i'm": 0.29; 'classes': 0.30; 'usually': 0.30; 'unlike': 0.30; 'code': 0.31; 'to:addr:python-list': 0.33; "can't": 0.34; 'pm,': 0.35; 'subject:?': 0.35; 'received:org': 0.36; 'really': 0.36; 'except': 0.36; 'but': 0.36; 'available.': 0.37; 'bad': 0.37; 'correctly': 0.37; 'quite': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'things': 0.38; 'nothing': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'most': 0.61; 'leading': 0.61; 'save': 0.61; 'provide': 0.62; 'more': 0.63; 'customized': 0.64; 'touch': 0.69; 'fact,': 0.69; 'yourself': 0.77; 'received:fios.verizon.net': 0.84; 'habit': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: how to use property? |
| Date | Mon, 17 Sep 2012 19:34:52 -0400 |
| References | <CAGYVzb1-D4xyCeYHL6R1_O8msDfn9pd=UcuYhno5i3HCkB_t=A@mail.gmail.com> <CAPTjJmrwctEeXfqmn1Aejqq8jR6p8-97Ur=E8E6PaeGe2o=+Rg@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | quoted-printable |
| X-Gmane-NNTP-Posting-Host | pool-173-75-251-66.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0 |
| In-Reply-To | <CAPTjJmrwctEeXfqmn1Aejqq8jR6p8-97Ur=E8E6PaeGe2o=+Rg@mail.gmail.com> |
| 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.845.1347924911.27098.python-list@python.org> (permalink) |
| Lines | 52 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1347924911 news.xs4all.nl 6946 [2001:888:2000:d::a6]:38403 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:29394 |
Show key headers only | View raw
On 9/17/2012 6:12 PM, Chris Angelico wrote:
> 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.
More examples:
A class has a data attribute that really is a simple attribute, no
property. You define a subclass that needs a calculation for the
attribute. So you use property in the subclass.
A class has an attribute that is a constant that must be computed. You
do not want to compute is unless and until needed.
def get_x(self):
try:
return self._x
except AttributeError:
self._x = calculate_x()
return self._
For a read-only attribute, don't provide a setter. If you do not like
"AttributeError: can't set attribute", provide one with a customized error.
But I think most of the data attributes in stdlib classes are straight
attributes.
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: how to use property? Terry Reedy <tjreedy@udel.edu> - 2012-09-17 19:34 -0400
csiph-web