浏览代码

[add] lazyProperty.py

jhao 6 年之前
父节点
当前提交
fa460d9fdc
共有 1 个文件被更改,包括 31 次插入0 次删除
  1. 31 0
      Util/lazyProperty.py

+ 31 - 0
Util/lazyProperty.py

@@ -0,0 +1,31 @@
+# -*- coding: utf-8 -*-
+"""
+-------------------------------------------------
+   File Name:     lazyProperty
+   Description :
+   Author :        JHao
+   date:          2016/12/3
+-------------------------------------------------
+   Change Activity:
+                   2016/12/3:
+-------------------------------------------------
+"""
+__author__ = 'JHao'
+
+
+class LazyProperty(object):
+    """
+    LazyProperty
+    explain: http://www.spiderpy.cn/blog/5/
+    """
+
+    def __init__(self, func):
+        self.func = func
+
+    def __get__(self, instance, owner):
+        if instance is None:
+            return self
+        else:
+            value = self.func(instance)
+            setattr(instance, self.func.__name__, value)
+            return value