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


Groups > comp.lang.python > #32576

Re: lazy properties?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <cameron@cskk.homeip.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'else:': 0.03; 'attribute': 0.05; 'none:': 0.05; 'that?': 0.05; 'caller': 0.07; '%s",': 0.09; "'''": 0.09; '@property': 0.09; 'cached': 0.09; 'compute': 0.09; 'fetch': 0.09; 'imply': 0.09; 'cc:addr :python-list': 0.10; 'def': 0.10; 'modification': 0.15; 'computes': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'id(self),': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'nesting': 0.16; 'received:202.125.174': 0.16; 'received:202.125.174.133': 0.16; 'received:boardofstudies.nsw.edu.au': 0.16; 'received:cskk.homeip.net': 0.16; 'received:edu.au': 0.16; 'received:harvey.boardofstudies.nsw.edu.au': 0.16; 'received:homeip.net': 0.16; 'received:nsw.edu.au': 0.16; 'simpson': 0.16; 'wrote:': 0.17; 'trying': 0.21; 'controlled': 0.22; 'object.': 0.22; 'cheers,': 0.23; 'cc:2**0': 0.23; "i've": 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:# 10': 0.27; 'first.': 0.27; 'skip:@ 10': 0.27; 'rest': 0.28; 'dan': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'function': 0.30; 'could': 0.32; 'problem': 0.33; 'code:': 0.33; 'subject:?': 0.35; 'similar': 0.35; 'but': 0.36; 'expensive': 0.36; 'received:au': 0.36; 'useful': 0.36; 'should': 0.36; 'charset:us-ascii': 0.36; 'level': 0.37; 'uses': 0.37; 'quite': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'skip:l 20': 0.38; 'some': 0.38; 'called': 0.39; 'help': 0.40; 'content-disposition:inline': 0.60; 'skip:u 10': 0.60; 'box.': 0.65; 'wish': 0.70; 'andrea': 0.84; 'cordless': 0.84; 'watches': 0.84
Date Fri, 2 Nov 2012 09:08:09 +1100
From Cameron Simpson <cs@zip.com.au>
To Andrea Crotti <andrea.crotti.0@gmail.com>
Subject Re: lazy properties?
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Disposition inline
In-Reply-To <5092EBED.2090002@gmail.com>
User-Agent Mutt/1.5.21 (2010-09-15)
References <5092EBED.2090002@gmail.com>
Cc python-list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3169.1351807702.27098.python-list@python.org> (permalink)
Lines 74
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351807702 news.xs4all.nl 6901 [2001:888:2000:d::a6]:58752
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32576

Show key headers only | View raw


On 01Nov2012 21:38, Andrea Crotti <andrea.crotti.0@gmail.com> 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?
| 
| 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..

I've got one of these which I use exactly as you wish above:

  def lazy_property(func):
    ''' A property whose access is controlled by a lock if unset.
    '''
    lock_name = '_lock'
    prop_name = '_' + func.__name__
    unset_object = None
    def getprop(self):
      ''' Attempt lockless fetch of property first.
          Use lock if property is unset.
      '''
      p = getattr(self, prop_name)
      if p is unset_object:
        with getattr(self, lock_name):
          p = getattr(self, prop_name)
          if p is unset_object:
            ##debug("compute %s...", prop_name)
            p = func(self)
            ##warning("compute %s[%s].%s: %s", self, id(self), prop_name,
  type(p))
            setattr(self, prop_name, p)
      return p
    return property(getprop)

It computes the cached property name from the function name, but uses a
global lock name "_lock" on the basis that the long_computation() will
use shared state with the rest of the object.

The microoptimisation of the lockless fetch may be either nonportable or
pointless.

I need to abstract this with a deeper level of nesting to support
chaning lock_name, prop_name and unset_object if the caller desires, but
for what you want it will work out of the box.

I've got a similar thing that watches files for modification and reloads
at need.

Cheers,
-- 
Cameron Simpson <cs@zip.com.au>

Cordless hoses have been around for quite some time. They're called buckets.
        - Dan Prener <prener@watson.ibm.com>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: lazy properties? Cameron Simpson <cs@zip.com.au> - 2012-11-02 09:08 +1100

csiph-web