Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: lazy properties?

Reply
Thread Tools

Re: lazy properties?

 
 
Stefan H. Holek
Guest
Posts: n/a
 
      11-02-2012
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
lazy evaluation is sometimes too lazy... help please. Ken Pu Python 3 01-16-2009 11:23 AM
Re: lazy evaluation is sometimes too lazy... help please. Boris Borcic Python 0 01-16-2009 10:46 AM
Re: lazy evaluation is sometimes too lazy... help please. Boris Borcic Python 0 01-16-2009 10:37 AM
hello! first post to clr. I'm asking about an attempt at a lazy rubysolution to computing fibonacci numbers for a project euler problem. seems tobe a bug in lazy ruby... tphyahoo Ruby 6 08-08-2008 08:15 PM
Reg Ex Help for a Lazy VB Programmer adams114@comcast.net Perl 6 04-21-2004 08:27 PM



Advertisments