Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.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; 'python,': 0.02; 'initialize': 0.07; 'method.': 0.07; 'odd': 0.07; '__init__': 0.09; 'attributes': 0.09; 'none)': 0.09; 'subject:using': 0.09; 'python': 0.11; 'def': 0.12; 'attributes,': 0.16; 'cleanly': 0.16; 'magic': 0.16; 'subject:class': 0.16; 'subject:member': 0.16; 'thought.': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'pfxlen:0': 0.19; 'seems': 0.21; 'header:User-Agent:1': 0.23; 'new,': 0.24; 'post': 0.26; 'least': 0.26; 'skip:_ 20': 0.27; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'thus': 0.29; "doesn't": 0.30; 'originally': 0.30; "i'm": 0.30; 'piece': 0.31; 'values.': 0.31; 'class': 0.32; 'handled': 0.32; 'one,': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'words,': 0.36; "didn't": 0.36; 'thanks': 0.36; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'improved': 0.60; 'simply': 0.61; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'idiom': 0.84; 'lazy': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=6NI6K5EgLb9pRsME0ijrNED8Khgj7eoWruFUMpJVk2Y=; b=KVvTyRZgtO14SO2M1Rai6gipchX3zFmXUobUGE+6AfKe+zfSWBxyLFBcd63TEd1LE6 suoDEFFFtQMEPg5BVzMpJ6Xb1vSp0b2yT9uTcG1tkJbxWctlV0mhpD9+eKaNKakiBLC9 pSLtjiTp/MuIAY4q7cPwMPHjDqddv/+Vc9HYcXgiAiEW5SEWJFCW6IhHA8j5MhnpaCnq bqY1zfrXLhj/1HCwnJs/b8FDF4JHvyQ4XsoifFy6u2nNQHVkE9Bo1RCJBoGST2hxM0Yw Q8JgsSfCKQNXRQ/hwRKXI/XtodWNxxdUGE0BAJM2n3GMi6jtlrZTCz8un9WFFUzg0RA2 3Zwg== X-Received: by 10.49.107.226 with SMTP id hf2mr13453397qeb.17.1382231634992; Sat, 19 Oct 2013 18:13:54 -0700 (PDT) Sender: Ned Batchelder Date: Sat, 19 Oct 2013 21:13:53 -0400 From: Ned Batchelder User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.0.1 MIME-Version: 1.0 To: Peter Cacioppi , python-list@python.org Subject: Re: skipping __init__ and using exploiting a class member instead References: In-Reply-To: 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 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1382231637 news.xs4all.nl 15996 [2001:888:2000:d::a6]:54173 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:57137 On 10/19/13 8:42 PM, Peter Cacioppi wrote: > To be clear, my original post had a goof. > > So my original, de-goofed, idiom was > > > class Foo (object) : > _lazy = None > def foo(self, x) : > self._lazy = self._lazy or self.get_something(x) > def get_something(self, x) : > # doesn't really matter, so long as it returns truthy result > > and the new, improved idiom is > > class Foo (object) : > def foo(self, x) : > self._lazy = getattr(self, "_lazy", None) or self._get_something(x) > def _get_something(self, x) : > # doesn't really matter, so long as it returns truthy result The use of getattr here seems unfortunate. Your original at least didn't have that odd uncertainty about it. I'm not sure why you want to avoid an __init__ method. Why not simply have one, and use it to initialize your attributes, even if it is to None? --Ned. > I was laboring under some misconception that there was Python magic that allowed __init__ and only __init__ to add class attributes by setting their values. Good to know this piece of magic isn't part of Python, and thus lazy eval can be handled more cleanly than I originally thought. > > In other words, "Guido was here". > > Thanks again >