Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: lazy properties? (http://www.velocityreviews.com/forums/t954120-re-lazy-properties.html)

Ian Kelly 11-01-2012 09:52 PM

Re: lazy properties?
 
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))

Miki Tebeka 11-01-2012 11:30 PM

Re: lazy properties?
 
> 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/...he-decorators/

Miki Tebeka 11-01-2012 11:30 PM

Re: lazy properties?
 
> 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/...he-decorators/


All times are GMT. The time now is 05:00 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57