title: Python memoising/deferred lookup property decorator
body: <p>Recently I've gone through an existing code base containing many classes where instance attributes reflect values stored in a database. I've refactored a lot of these attributes to have their database lookups be deferred, ie. not be initialised in the constructor but only upon first read. These attributes do not change over the lifetime of the instance, but they're a real bottleneck to calculate that first time and only really accessed for special cases. Hence they can also be cached after they've been retrieved from the database (this therefore fits the definition of <em>memoisation</em> where the input is simply "no input").</p>
  
  <p>I find myself typing the following snippet of code over and over again for various attributes across various classes:</p>
  
  <pre><code>class testA(object):
  
    def __init__(self):
      self._a = None
      self._b = None
  
    @property
    def a(self):
      if self._a is None:
        # Calculate the attribute now
        self._a = 7
      return self._a
  
    @property
    def b(self):
      #etc
  </code></pre>
  
  <p>Is there an existing decorator to do this already in Python that I'm simply unaware of? Or, is there a reasonably simple way to define a decorator that does this?</p>
  
  <p>I'm working under Python 2.5, but 2.6 answers might still be interesting if they are significantly different.</p>
  
  <h2>Note</h2>
  
  <p>This question was asked before Python included a lot of ready-made decorators for this. I have updated it only to correct terminology.</p>