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


Groups > comp.lang.python > #32573 > unrolled thread

Re: lazy properties?

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2012-11-01 15:52 -0600
Last post2012-11-01 16:30 -0700
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#32573 — Re: lazy properties?

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-11-01 15:52 -0600
SubjectRe: lazy properties?
Message-ID<mailman.3168.1351806771.27098.python-list@python.org>
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))

[toc] | [next] | [standalone]


#32580

FromMiki Tebeka <miki.tebeka@gmail.com>
Date2012-11-01 16:30 -0700
Message-ID<6efda4fe-4c69-439b-a2ae-8bed20ce08df@googlegroups.com>
In reply to#32573
> If you're using Python 3.2+, then functools.lru_cache probably
> ...
And if you're on 2.X, you can grab lru_cache from http://code.activestate.com/recipes/498245-lru-and-lfu-cache-decorators/

[toc] | [prev] | [next] | [standalone]


#32581

FromMiki Tebeka <miki.tebeka@gmail.com>
Date2012-11-01 16:30 -0700
Message-ID<mailman.3172.1351812652.27098.python-list@python.org>
In reply to#32573
> If you're using Python 3.2+, then functools.lru_cache probably
> ...
And if you're on 2.X, you can grab lru_cache from http://code.activestate.com/recipes/498245-lru-and-lfu-cache-decorators/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web