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


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

Re: lazy properties?

Started by"Stefan H. Holek" <stefan@epy.co.at>
First post2012-11-02 09:36 +0100
Last post2012-11-02 09:36 +0100
Articles 1 — 1 participant

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? "Stefan H. Holek" <stefan@epy.co.at> - 2012-11-02 09:36 +0100

#32601 — Re: lazy properties?

From"Stefan H. Holek" <stefan@epy.co.at>
Date2012-11-02 09:36 +0100
SubjectRe: lazy properties?
Message-ID<mailman.3183.1351845374.27098.python-list@python.org>
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

[toc] | [standalone]


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


csiph-web