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


Groups > comp.lang.python > #32572

lazy properties?

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <andrea.crotti.0@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; 'else:': 0.03; 'attribute': 0.05; 'none:': 0.05; 'that?': 0.05; '@property': 0.09; 'compute': 0.09; 'imply': 0.09; 'def': 0.10; 'to:name:python-list': 0.15; 'trying': 0.21; 'header:User- Agent:1': 0.26; 'skip:@ 10': 0.27; 'received:209.85.212': 0.28; 'received:192.168.1.3': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'could': 0.32; 'problem': 0.33; 'to:addr:python-list': 0.33; 'code:': 0.33; 'received:google.com': 0.34; 'subject:?': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'message-id:@gmail.com': 0.36; 'expensive': 0.36; 'useful': 0.36; 'should': 0.36; 'quite': 0.37; 'received:209': 0.37; 'far': 0.37; 'some': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'called': 0.39; 'received:192.168': 0.40; 'header:Received:5': 0.40; 'help': 0.40
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=epBUHeQWXA5tDspOrjWuv/18/MNvhcAxoFIYuX5ZEv4=; b=KPl96kU2Vkp29Fx2zOHnALYUTtbcgzLet6mjedhfhoMw53yXjtv+Gj5ytd/HbsMfTH /toz6BVOCdelP6W7O9FmWp8EtzrHTTeEM6/YCgXrHoAO/+05pWWzuNgj7Zs1/S+aNIuE 5XQ76ON8jtbpAeRMPzMjTY2+VMGVTPURW296QnNvdyvG9yxcdDlkR2Jl+0GMl+DPtfg/ nUYiJuARyJVzQcKtHVSkU1txmXD0qsyeexk7sOxLO6f1oUiKEUuL6zm2/0YDopP4zX6e pp2I7FdXHjfp79GvNmymARnMqeErZGwHJDBzjtlGqHwShgvBS8hfq9Wp+vgzLQ8N/4i0 8hHA==
Date Thu, 01 Nov 2012 21:38:53 +0000
From Andrea Crotti <andrea.crotti.0@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121012 Thunderbird/16.0.1
MIME-Version 1.0
To python-list <python-list@python.org>
Subject lazy properties?
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3165.1351806008.27098.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351806008 news.xs4all.nl 6928 [2001:888:2000:d::a6]:58016
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32572

Show key headers only | View raw


Seeing the wonderful "lazy val" in Scala I thought that I should try to 
get the following also in Python.
The problem is that I often have this pattern in my code:

class Sample:
     def __init__(self):
         self._var = None

     @property
     def var(self):
         if self._var is None:
             self._var = long_computation()
         else:
             return self._var


which is quite useful when you have some expensive attribute to compute 
that is not going to change.
I was trying to generalize it in a @lazy_property but my attempts so far 
failed, any help on how I could do that?

What I would like to write is
     @lazy_property
     def var_lazy(self):
         return long_computation()

and this should imply that the long_computation is called only once..

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


Thread

lazy properties? Andrea Crotti <andrea.crotti.0@gmail.com> - 2012-11-01 21:38 +0000

csiph-web