Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'languages.': 0.04; 'attribute': 0.07; 'initialize': 0.07; 'none:': 0.07; 'practice,': 0.07; '__init__': 0.09; 'attributes': 0.09; 'form?': 0.09; 'namespace': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:using': 0.09; 'python': 0.11; 'def': 0.12; 'attribute,': 0.16; 'attribute?': 0.16; 'fallback': 0.16; 'fits': 0.16; 'foo(object):': 0.16; 'kern': 0.16; 'lazily': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:class': 0.16; 'subject:member': 0.16; 'underlying': 0.16; 'wrote:': 0.18; "python's": 0.19; 'saying': 0.22; 'header:User- Agent:1': 0.23; 'interpret': 0.24; 'task': 0.26; 'this:': 0.26; 'certain': 0.27; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'wondering': 0.29; 'feature': 0.29; '[1]': 0.29; 'raise': 0.29; "doesn't": 0.30; 'characters': 0.30; 'compared': 0.30; 'robert': 0.30; 'said,': 0.30; 'fine,': 0.31; 'relies': 0.31; 'class': 0.32; 'sense': 0.34; 'skip:_ 10': 0.34; 'something': 0.35; 'but': 0.35; 'really': 0.36; 'c++': 0.36; 'keyword': 0.36; 'experience,': 0.37; 'positive': 0.37; 'two': 0.37; 'being': 0.38; 'sometimes': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'voice': 0.60; 'matter': 0.61; 'hear': 0.63; 'our': 0.64; 'more': 0.64; 'world': 0.66; 'between': 0.67; 'believe': 0.68; 'past.': 0.68; 'default': 0.69; 'batchelder': 0.84; 'different.': 0.84; 'eco': 0.84; 'idiom': 0.84; 'situations,': 0.84; 'terrible': 0.84; 'x):': 0.84; 'received:86': 0.91; 'hand,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Robert Kern Subject: Re: skipping __init__ and using exploiting a class member instead Date: Sat, 19 Oct 2013 23:18:10 +0100 References: <5262FFED.9010309@nedbatchelder.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: cpc2-cmbg17-2-0-cust347.5-4.cable.virginmedia.com User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 In-Reply-To: <5262FFED.9010309@nedbatchelder.com> 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: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1382221104 news.xs4all.nl 15904 [2001:888:2000:d::a6]:51875 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:57122 On 2013-10-19 22:55, Ned Batchelder wrote: > On 10/19/13 5:44 PM, Peter Cacioppi wrote: >> Is the following considered poor Python form? >> >> class Foo (object) : >> _lazy = None >> def foo(self, x) : >> _lazy = _lazy or self.get_something(x) >> def get_something(self, x) : >> # doesn't really matter >> >> I like this idiom for certain situations, just wondering if it will raise the >> hackles of other Pythonistas. >> >> I use this idiom sparingly, but sometimes it just fits the task at hand, I >> hear Guidos voice saying "use the Force" in my ear, etc. > > You present this as a choice between __init__ or a class attribute, but those > two things are very different. Is your intent to have an instance attribute, or > a class attribute? Lazily populated instance attributes are fine, I would do it > like this: > > class Foo(object): > def __init__(self): > self._lazy = None > > def foo(self, x): > if self._lazy is None: > self._lazy = self.get_something(x) > ... I think he left some important characters out. class Foo (object) : _lazy = None def foo(self, x) : self._lazy = self._lazy or self.get_something(x) # Use self._lazy for something def get_something(self, x) : # doesn't really matter The main difference being that he doesn't initialize the instance attribute and just relies on the fallback to class attribute lookup. In my experience, using a static[1] class attribute as a default for an instance attribute is accepted practice, and one that gets touted as a positive feature of Python's namespace model when compared against other languages. That said, I have seen it more often in the past. [1] In the general "unchanging" sense rather than the C++ "static" keyword sense. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco