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


Groups > comp.lang.python > #29397

Re: how to use property?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
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; 'attributes': 0.07; 'read-only': 0.07; 'try:': 0.07; 'python': 0.09; 'attribute.': 0.09; 'compute': 0.09; 'guys!': 0.09; 'sep': 0.09; 'subclass': 0.09; 'terry': 0.09; 'def': 0.10; 'language': 0.14; 'java,': 0.15; 'attribute,': 0.16; 'attributes.': 0.16; 'examples:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'java.': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'properties.': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reedy': 0.16; 'used:': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'define': 0.20; 'error.': 0.21; 'constant': 0.22; 'needed.': 0.23; "i've": 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'c++': 0.27; 'chris': 0.28; 'decide': 0.28; '"in': 0.29; '>>>>': 0.29; 'received:192.168.1.3': 0.29; 'class': 0.29; "i'm": 0.29; 'classes': 0.30; 'code': 0.31; 'received:84': 0.32; 'to:addr :python-list': 0.33; "can't": 0.34; 'needed': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'really': 0.36; 'except': 0.36; 'but': 0.36; 'bad': 0.37; 'correctly': 0.37; 'does': 0.37; 'quite': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'think': 0.40; 'most': 0.61; 'provide': 0.62; 'more': 0.63; 'customized': 0.64; 'header:Reply- To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'delphi': 0.84; 'reply-to:addr:python.org': 0.84; 'habit': 0.91; 'angel': 0.93
X-CM-Score 0.00
X-CNFS-Analysis v=2.0 cv=IekFqBWa c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=AAvI7MrX_rgA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=ypjUn0TCmEsA:10 a=pGLkceISAAAA:8 a=mvHagkWQN481NRHoK8cA:9 a=QEXdDO2ut3YA:10 a=MSl-tDqOz04A:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Tue, 18 Sep 2012 01:26:44 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20120907 Thunderbird/15.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: how to use property?
References <CAGYVzb1-D4xyCeYHL6R1_O8msDfn9pd=UcuYhno5i3HCkB_t=A@mail.gmail.com> <CAPTjJmrwctEeXfqmn1Aejqq8jR6p8-97Ur=E8E6PaeGe2o=+Rg@mail.gmail.com> <k38c2u$q28$1@ger.gmane.org> <5057B64F.2080500@davea.name>
In-Reply-To <5057B64F.2080500@davea.name>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To python-list@python.org
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.848.1347928001.27098.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1347928001 news.xs4all.nl 6957 [2001:888:2000:d::a6]:56226
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29397

Show key headers only | View raw


On 2012-09-18 00:46, Dave Angel wrote:
> On 09/17/2012 07:34 PM, Terry Reedy wrote:
>> 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?
>>>
>> <snip>
>
>> 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.
>>
>
> An important difference from every other language I've used:  The user
> of the attribute does not need to change his code when you decide it
> needs reimplementation as a property.
>
> In C++ and java, for example, people will define getter and setter
> methods just so they don't have to change them later.  Just "in case"
> it's needed later.
>
C# and Delphi (IIRC) also support properties.

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


Thread

Re: how to use property? MRAB <python@mrabarnett.plus.com> - 2012-09-18 01:26 +0100

csiph-web