I&#x27;ve previously used hasattr() to check for the existence of an attribute qua instance variable to lazy-load and cache that attribute<p><pre><code>    def kdtree(self):
      if not hasattr(self, &#x27;_kdtree&#x27;):
        self._kdtree = scipy.spatial.KDTree(self.xi.T)
      return self._kdtree

</code></pre>
like ruby&#x27;s<p><pre><code>    @foo ||= expensive_calculation
</code></pre>
is there a better way?