lazy_property.py 343 B

123456789101112
  1. class lazy_property():
  2. """Defines a property whose value will be computed only once and as needed.
  3. This can only be used on instance methods.
  4. """
  5. def __init__(self, func):
  6. self._func = func
  7. def __get__(self, obj_self, cls):
  8. value = self._func(obj_self)
  9. setattr(obj_self, self._func.__name__, value)
  10. return value