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


Groups > comp.lang.python > #32601

Re: lazy properties?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <stefan@epy.co.at>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'else:': 0.03; 'url:pypi': 0.03; 'attribute': 0.05; 'none:': 0.05; 'that?': 0.05; 'decorator': 0.07; '@property': 0.09; 'compute': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'wrote:': 0.17; 'stefan': 0.17; 'trying': 0.21; 'cc:2**0': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'skip:@ 10': 0.27; 'received:192.168.1.3': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'url:python': 0.32; 'could': 0.32; 'problem': 0.33; 'code:': 0.33; 'subject:?': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'expensive': 0.36; 'useful': 0.36; 'should': 0.36; 'charset:us-ascii': 0.36; 'quite': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'received:192': 0.39; 'received:192.168': 0.40; 'help': 0.40; 'header:Message-Id:1': 0.62; 'received:at': 0.71; 'andrea': 0.84
Subject Re: lazy properties?
Mime-Version 1.0 (Apple Message framework v1085)
Content-Type text/plain; charset=us-ascii
From "Stefan H. Holek" <stefan@epy.co.at>
In-Reply-To <5092EBED.2090002@gmail.com>
Date Fri, 2 Nov 2012 09:36:11 +0100
Content-Transfer-Encoding quoted-printable
References <5092EBED.2090002@gmail.com>
To Andrea Crotti <andrea.crotti.0@gmail.com>
X-Mailer Apple Mail (2.1085)
Cc python-list <python-list@python.org>
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.3183.1351845374.27098.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351845374 news.xs4all.nl 6983 [2001:888:2000:d::a6]:33825
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32601

Show key headers only | View raw


On 01.11.2012, at 22:38, Andrea Crotti wrote:

> 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?

There is a ready made and well tested lazy decorator at http://pypi.python.org/pypi/lazy

Stefan

-- 
Stefan H. Holek
stefan@epy.co.at

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


Thread

Re: lazy properties? "Stefan H. Holek" <stefan@epy.co.at> - 2012-11-02 09:36 +0100

csiph-web