ソースを参照

[update] add LazyProperty

jhao104 9 年 前
コミット
bcc5eab427
1 ファイル変更31 行追加0 行削除
  1. 31 0
      Util/utilClass.py

+ 31 - 0
Util/utilClass.py

@@ -0,0 +1,31 @@
+# -*- coding: utf-8 -*-
+"""
+-------------------------------------------------
+   File Name:     utilClass.py  
+   Description :  tool class
+   Author :       JHao
+   date:          2016/12/3
+-------------------------------------------------
+   Change Activity:
+                   2016/12/3: Class LazyProperty
+-------------------------------------------------
+"""
+__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