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


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

Re: how to use property?

Started byMRAB <python@mrabarnett.plus.com>
First post2012-09-18 01:26 +0100
Last post2012-09-18 01:26 +0100
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? MRAB <python@mrabarnett.plus.com> - 2012-09-18 01:26 +0100

#29397 — Re: how to use property?

FromMRAB <python@mrabarnett.plus.com>
Date2012-09-18 01:26 +0100
SubjectRe: how to use property?
Message-ID<mailman.848.1347928001.27098.python-list@python.org>
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.

[toc] | [standalone]


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


csiph-web