Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #57117
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.013 |
| X-Spam-Evidence | '*H*': 0.97; '*S*': 0.00; 'none:': 0.07; '__init__': 0.09; 'attributes': 0.09; 'form?': 0.09; 'subject:using': 0.09; 'python': 0.11; 'def': 0.12; 'attribute,': 0.16; 'attribute?': 0.16; 'fits': 0.16; 'foo(object):': 0.16; 'lazily': 0.16; 'subject:class': 0.16; 'subject:member': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'pfxlen:0': 0.19; 'saying': 0.22; 'header:User-Agent:1': 0.23; 'task': 0.26; 'this:': 0.26; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'wondering': 0.29; 'raise': 0.29; "doesn't": 0.30; 'fine,': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'two': 0.37; 'sometimes': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'voice': 0.60; 'matter': 0.61; 'hear': 0.63; 'to:addr:gmail.com': 0.65; 'between': 0.67; 'different.': 0.84; 'idiom': 0.84; 'situations,': 0.84; 'x):': 0.84; 'hand,': 0.93 |
| 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=B1NDl0malmniBLNYiJgoBEFmQjQ4WfeyCGavBhw/2bM=; b=neSIS5PRC71shOGKMOHVCj/lH60B0Whi9Ltedqt2+vNKVt2VxZjxWljY/fAIFVCZDG +MQUZxMIWFMlcnwD3KNSq4lqSP9eM59xXSPAb48/FlHxYvq9fYdOetS7DB50sziMQHRq RuCaQi+DG6g5ngjVCPXL+msAa2C3gCRpBVcocIdxlr2X0SPyHjRVOR/UVFCCoK5waq4a Q+MH9SZ1xddXxf4n+YqcN27KIZ/nBFFT1NOI5POel3lfV1HAh/6li9szV8rHUTLqIoxK 8mJnGp3Dr+QpFvOYJGmSIvyPlLmff3pPFGYshKRsal7Pkf2TLzg1dAjKOh0p6u9dT50c 88DQ== |
| X-Received | by 10.224.80.4 with SMTP id r4mr13572507qak.69.1382219756717; Sat, 19 Oct 2013 14:55:56 -0700 (PDT) |
| Sender | Ned Batchelder <nedbat@gmail.com> |
| Date | Sat, 19 Oct 2013 17:55:57 -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> |
| In-Reply-To | <fb1b5b6f-619c-4acd-b1bf-a375ccc6f1a6@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.1266.1382219758.18130.python-list@python.org> (permalink) |
| Lines | 29 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1382219758 news.xs4all.nl 15878 [2001:888:2000:d::a6]:36681 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:57117 |
Show key headers only | View raw
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)
...
--Ned.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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