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/t954135-re-lazy-properties.html)

Stefan H. Holek 11-02-2012 08:36 AM

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



All times are GMT. The time now is 01:30 PM.

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