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


Groups > comp.lang.python > #32573

Re: lazy properties?

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.039
X-Spam-Evidence '*H*': 0.92; '*S*': 0.00; 'python': 0.09; '@property': 0.09; 'imply': 0.09; 'def': 0.10; 'shorten': 0.16; 'wrote:': 0.17; 'thu,': 0.17; 'skip:p 30': 0.20; 'header:In-Reply- To:1': 0.25; 'skip:@ 10': 0.27; 'message-id:@mail.gmail.com': 0.27; 'email name:': 0.29; 'probably': 0.29; 'received:209.85.215.46': 0.30; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'nov': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'really': 0.36; 'should': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'andrea': 0.84; 'to:name:python': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=LbXUEKRlSXUbHMIJ4tvvYf9jdJ3J8Xsho9HsyDG0Lpo=; b=H1ZTvq3E7pw00AVAO8e7vfwYIQBI9EtuW3RT35HBhH3sFjPS1H17fCkXtuFV51r22Z MKuiBqNbdYvdJ97klVvkUlHBwZibIlp5Rq/JKH9TFQHSYpF6U5RAdiJSl/PzfW0oVwX8 82pj69dRcl5NA7w7q+eg/ymxrVTJmqRTzipKuRuEKVAwk8cjqC/exLwyPAmqKAd8o2cV DEO9up3aZ2tV88iaO3t/AQVzCxKV4x7W91h5Waqn+bIbZdCThlMnNaPpEnxDeBoWq1Dl TotAXiuUP5UEkTBXNTRWDsSmi1hCr2il1zSfi9m8U6n0TEX6aw3+16GdSELQLyDeQnEq KjmA==
MIME-Version 1.0
In-Reply-To <5092EBED.2090002@gmail.com>
References <5092EBED.2090002@gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 1 Nov 2012 15:52:17 -0600
Subject Re: lazy properties?
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
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.3168.1351806771.27098.python-list@python.org> (permalink)
Lines 20
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351806771 news.xs4all.nl 6842 [2001:888:2000:d::a6]:41355
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32573

Show key headers only | View raw


On Thu, Nov 1, 2012 at 3:38 PM, Andrea Crotti <andrea.crotti.0@gmail.com> wrote:
> 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..

If you're using Python 3.2+, then functools.lru_cache probably
suffices for your needs.

@property
@functools.lru_cache()
def var_lazy(self):
    return long_computation()

If you really need to shorten that to a single declaration:

def lazy_property(func):
    return property(functools.lru_cache()(func))

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


Thread

Re: lazy properties? Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-01 15:52 -0600
  Re: lazy properties? Miki Tebeka <miki.tebeka@gmail.com> - 2012-11-01 16:30 -0700
  Re: lazy properties? Miki Tebeka <miki.tebeka@gmail.com> - 2012-11-01 16:30 -0700

csiph-web