Re: lazy properties?

Discussion in 'Python' started by Stefan H. Holek, Nov 2, 2012.

  1. 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 H. Holek, Nov 2, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Pu
    Replies:
    3
    Views:
    671
    Steven D'Aprano
    Jan 16, 2009
  2. Boris Borcic
    Replies:
    0
    Views:
    547
    Boris Borcic
    Jan 16, 2009
  3. Boris Borcic
    Replies:
    0
    Views:
    545
    Boris Borcic
    Jan 16, 2009
  4. Andrea Crotti

    lazy properties?

    Andrea Crotti, Nov 1, 2012, in forum: Python
    Replies:
    0
    Views:
    162
    Andrea Crotti
    Nov 1, 2012
  5. Ian Kelly

    Re: lazy properties?

    Ian Kelly, Nov 1, 2012, in forum: Python
    Replies:
    2
    Views:
    156
    Miki Tebeka
    Nov 1, 2012
Loading...

Share This Page