您要使用
@property装饰器。创建一个像普通属性一样可以访问的方法,该方法可以进行延迟计算:
class SampleObject: def __init__(self): # ... self._total = None @property def total(self): """Compute or return the _total attribute.""" if self._total is None: self.compute_total() return self._total



