Return-Path: 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; 'python.': 0.02; 'subject:Python': 0.06; 'attribute': 0.07; 'method.': 0.07; 'suppose': 0.07; 'alternatives': 0.09; 'obj': 0.09; 'python': 0.11; 'def': 0.12; 'creates': 0.14; 'attributes,': 0.16; 'fits': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'instantiated': 0.16; 'length)': 0.16; 'length.': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'public,': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'wrote:': 0.18; 'users.': 0.18; 'previously': 0.22; 'header :User-Agent:1': 0.23; 'necessary.': 0.24; 'class.': 0.26; 'header :In-Reply-To:1': 0.27; 'easier': 0.31; 'piece': 0.31; 'class': 0.32; 'received:84': 0.35; 'but': 0.35; 'there': 0.35; '(e.g.,': 0.36; 'i.e.': 0.36; 'options:': 0.36; 'method': 0.36; 'two': 0.37; 'expected': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'called': 0.40; 'users': 0.40; 'new': 0.61; 'first': 0.61; 'information': 0.63; 'such': 0.63; 'provide': 0.64; 'more': 0.64; 'deals': 0.65; 'within': 0.65; 'header:Reply- To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'information:': 0.72; 'potentially': 0.81; 'reply-to:addr:python.org': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=ZMDuxxLb c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=0kkAYlmtguIA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=RnqSfMZ_l9EA:10 a=W8xr5IaSuGQBU3VAaU4A:9 a=tkrSZkk8FLTP0LVr:21 a=rEGfNwTsYL5QWPo8:21 a=wPNLvfGTeEIA:10 X-AUTH: mrabarnett:2500 Date: Sat, 17 Aug 2013 18:10:25 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Python getters and setters References: <227bd47c-0e86-4d65-985f-e59aa4f25294@googlegroups.com> In-Reply-To: <227bd47c-0e86-4d65-985f-e59aa4f25294@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376759432 news.xs4all.nl 15987 [2001:888:2000:d::a6]:56418 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:52636 On 17/08/2013 17:53, Fernando Saldanha wrote: > I am new to Python. > > I understand that it is "unpythonic" to write getters and setters, and that property() can be used if necessary. > > This deals with the case of attributes, but there are other kinds of information available within a class. > > Suppose my class contains an attribute called "data" that can potentially provide a lot of information that will be needed by class users. I have two options: > > 1) For each piece of information within data (e.g., length) I write a method that retrieves that information: > > def data_length(self): > return len(self.data) > > 2) I do not create such a method. Users that are interested in that information will have to write len(obj.data), where obj is a previously instantiated object of my class. > > Which one of the two alternatives fits better with the Python philosophy? The first alternative is more work for me, creates a "heavier" class and may have slower performance, but makes things easier for the user and is more implementation independent. > If the attribute is public, i.e. the user is expected to write obj.data, then len(obj.data) is the right way to get its length.