Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'cc:addr:python-list': 0.10; 'def': 0.10; 'language': 0.14; 'java,': 0.15; 'attribute,': 0.16; 'attributes.': 0.16; 'examples:': 0.16; 'java.': 0.16; 'reedy': 0.16; 'used:': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'define': 0.20; 'error.': 0.21; 'constant': 0.22; 'cc:2**0': 0.23; 'needed.': 0.23; "i've": 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; '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; 'class': 0.29; "i'm": 0.29; 'classes': 0.30; 'code': 0.31; "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; '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; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'habit': 0.91 Date: Mon, 17 Sep 2012 19:46:23 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: Terry Reedy Subject: Re: how to use property? References: In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Provags-ID: V02:K0:4xZVToshEmQHuGpet7JdXrtNZ6H9hkgIYU1AD9jHJhw 427N2QKPRb137fuECwW0fJAFYfy4g4nnJG7F7Tbkj57kd27lzW LhwCOY+FHeGHs4Ld+/PeYGwUTkAfekWwNZOKISMDFGu+d6XsMZ BSKuma5fDpjP5TeNL24E4YLkM3IWZUqCPEvMvYypLryAZqpw6j aLCNin0FPGLg9IsChJwAUox0lAMs3HAu7UB/ud8gp18T8aQtoa M1FN/qyTM47n8InDxfVTpQIIriznWrHwgEgvZnKWjPm+iqPAwV 7VX42dXZNfh4MpZUvf9jmd3HXCoxGDX+gVJfObQoGYgqTIkSQ= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347925615 news.xs4all.nl 6922 [2001:888:2000:d::a6]:43205 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29396 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 >> 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? >> > > 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. -- DaveA