Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #57137

Re: skipping __init__ and using exploiting a class member instead

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 <nedbat@gmail.com>
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 <nedbat@gmail.com>
Date Sat, 19 Oct 2013 21:13:53 -0400
From Ned Batchelder <ned@nedbatchelder.com>
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 <peter.cacioppi@gmail.com>, python-list@python.org
Subject Re: skipping __init__ and using exploiting a class member instead
References <fb1b5b6f-619c-4acd-b1bf-a375ccc6f1a6@googlegroups.com> <f33e6804-fa2f-4301-9bcd-222faf643ac9@googlegroups.com>
In-Reply-To <f33e6804-fa2f-4301-9bcd-222faf643ac9@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
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1277.1382231637.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


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
>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-19 14:44 -0700
  Re: skipping __init__ and using exploiting a class member instead Ned Batchelder <ned@nedbatchelder.com> - 2013-10-19 17:55 -0400
  Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-19 15:11 -0700
  Re: skipping __init__ and using exploiting a class member instead Robert Kern <robert.kern@gmail.com> - 2013-10-19 23:18 +0100
  Re: skipping __init__ and using exploiting a class member instead Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-19 22:30 +0000
  Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-19 17:07 -0700
  Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-19 17:42 -0700
    Re: skipping __init__ and using exploiting a class member instead Ned Batchelder <ned@nedbatchelder.com> - 2013-10-19 21:13 -0400
    Detecting whether a value was passed for a parameter (was: skipping __init__ and using exploiting a class member instead) Ben Finney <ben+python@benfinney.id.au> - 2013-10-20 22:50 +1100
  Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-20 00:09 -0700
    Re: skipping __init__ and using exploiting a class member instead Roy Smith <roy@panix.com> - 2013-10-20 08:55 -0400
    Re: skipping __init__ and using exploiting a class member instead Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-20 14:16 +0100
  Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-20 01:46 -0700
  Re: skipping __init__ and using exploiting a class member instead Devin Jeanpierre <jeanpierreda@gmail.com> - 2013-10-20 07:27 -0700
  Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-20 10:57 -0700
    Re: skipping __init__ and using exploiting a class member instead Roy Smith <roy@panix.com> - 2013-10-20 16:44 -0400
    Re: skipping __init__ and using exploiting a class member instead Chris Angelico <rosuav@gmail.com> - 2013-10-21 07:57 +1100
      Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-20 14:34 -0700
        Re: skipping __init__ and using exploiting a class member instead Roy Smith <roy@panix.com> - 2013-10-20 18:31 -0400
          Re: skipping __init__ and using exploiting a class member instead Ben Finney <ben+python@benfinney.id.au> - 2013-10-21 10:55 +1100
            Re: skipping __init__ and using exploiting a class member instead Neil Cerutti <neilc@norwich.edu> - 2013-10-21 18:47 +0000
  Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-20 21:30 -0700
  Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-20 21:31 -0700
    Re: skipping __init__ and using exploiting a class member instead feedthetroll@gmx.de - 2013-10-21 00:16 -0700

csiph-web