I've previously used hasattr() to check for the existence of an attribute qua instance variable to lazy-load and cache that attribute
def kdtree(self):
if not hasattr(self, '_kdtree'):
self._kdtree = scipy.spatial.KDTree(self.xi.T)
return self._kdtree
like ruby's @foo ||= expensive_calculation
is there a better way?