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; 'attribute': 0.07; 'method.': 0.07; '"if': 0.09; 'subject:method': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; 'assume': 0.14; 'suggest': 0.14; 'changes': 0.15; '@property': 0.16; 'attribute,': 0.16; 'boolean': 0.16; 'fits': 0.16; 'query,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'tests': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'advise.': 0.24; 'instance,': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'define': 0.26; 'updating': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'properties': 0.29; 'dec': 0.30; 'mix': 0.30; 'message-id:@mail.gmail.com': 0.30; 'that.': 0.31; 'aligned': 0.31; 'calculated': 0.31; 'post.': 0.31; 'class': 0.32; 'option': 0.32; 'received:209.85.212': 0.32; 'cases': 0.33; 'fri,': 0.33; 'sense': 0.34; 'received:209.85': 0.35; 'december': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'method': 0.36; 'thanks': 0.36; 'subject:?': 0.36; 'should': 0.36; 'received:209': 0.37; 'little': 0.38; 'does': 0.39; '12,': 0.39; 'simply': 0.61; "you're": 0.61; 'to:addr:gmail.com': 0.65; 'obvious': 0.74; 'subject:skip:w 20': 0.91 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=0Qads1TFAxMdp5kOq1eHofbVSF5hLW3VW673akpbPkg=; b=T85dbC93WHVmT9sI7dNOLTjiUUgTLhjhkJvqatfykGpCxofKxxGgDnqQGmeSPYdp0P x+m9xY5RhfNtggzGAqg+EdotT9E0sGufBlZ0JXby28OpnS+Nz2FKRSW/0evEc38Tw6ZJ dbW03u9MWzrK0ZM7ZmE8gOfgJyIhdrA17MlH64W1PnZKqroj7BkvlnhHYWBGXKsau5b/ KT8IyVXvyVQrS21FTpNTh1tTai5QIXOWAs64HHw6LOVDuAgcfO/hdtR9Xiqdu9DQUrU2 WBl3L9kX3Mmjmtzximnak8UY3iMgAoVXfiqx6gSdz1ldU+EnPEQdNTsB31WdtECgapKm zEsQ== X-Gm-Message-State: ALoCoQk5Gng8Gsc7sBn1/mewl77szcje0ieHKrwPDW7BcUgfn4riGQ5fBt3YCk2+k4jVMyUgkJLe X-Received: by 10.180.93.37 with SMTP id cr5mr6799661wib.76.1418383295862; Fri, 12 Dec 2014 03:21:35 -0800 (PST) MIME-Version: 1.0 X-Originating-IP: [82.110.183.162] In-Reply-To: References: From: Mateusz Loskot Date: Fri, 12 Dec 2014 12:21:05 +0100 Subject: Re: is_ as method or property? To: Chris Angelico Content-Type: text/plain; charset=UTF-8 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418383298 news.xs4all.nl 2847 [2001:888:2000:d::a6]:58361 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:82437 On 11 December 2014 at 19:20, Chris Angelico wrote: > On Fri, Dec 12, 2014 at 4:34 AM, Mateusz Loskot wrote: >> If a class member function simply tests something and >> returns a b::oolean call it >> >> def is_(): >> pass >> >> like 'is_even'. >> >> Should I define it as a classic method >> >> def is_even(self): >> pass >> >> or as a property >> >> @property >> def is_even(self): >> pass > > A property should be used if what you're creating is "virtually an > attribute". If it would make sense to have an attribute is_even, then > a property is_even makes sense. If in doubt, go with a method. Thanks for the advise. I've got several cases which are not obvious to me. For instance, class Foo has a boolean attribute, read-write, which I see a couple of realisations for possible: 1) properties only class Foo: @property def is_default(self): pass @is_default.setter def is_default(self, current): pass 2) property + method mix class Foo: @property def is_default(self): pass def make_default(self, current): pass 3) methods only class Foo: def is_default(self): pass def make_default(self, current): pass >From one angle, that is is_default is known and does not need to be calculated upon every query, the option 1) fits well. It is aligned with Ethan's suggestion in the other post. >From other point, let's assume updating it takes a little bit of changes of state of Foo instance, then perhaps methods-only option 3) fits best. Looks like "if in doubt, go with a method" is the safest bet. 'Statistics' from Python codebase also seem to suggest that. Best regards, -- Mateusz Loskot, http://mateusz.loskot.net