lazyProperty.py 749 B

12345678910111213141516171819202122232425262728293031
  1. # -*- coding: utf-8 -*-
  2. """
  3. -------------------------------------------------
  4. File Name: lazyProperty
  5. Description :
  6. Author : JHao
  7. date: 2016/12/3
  8. -------------------------------------------------
  9. Change Activity:
  10. 2016/12/3:
  11. -------------------------------------------------
  12. """
  13. __author__ = 'JHao'
  14. class LazyProperty(object):
  15. """
  16. LazyProperty
  17. explain: http://www.spiderpy.cn/blog/5/
  18. """
  19. def __init__(self, func):
  20. self.func = func
  21. def __get__(self, instance, owner):
  22. if instance is None:
  23. return self
  24. else:
  25. value = self.func(instance)
  26. setattr(instance, self.func.__name__, value)
  27. return value