changeset 107:b25bf8fcd197

created
author 127.0.0.1
date Fri, 06 Dec 2013 23:49:19 +0100
parents 5e763a12e171
children 04a46e901b55
files cached_property.py
diffstat 1 files changed, 19 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cached_property.py	Fri Dec 06 23:49:19 2013 +0100
@@ -0,0 +1,19 @@
+class cached_property(object):
+    def __init__(self, func):
+        self.func = func
+
+    def __get__(self, instance, type):
+        if instance is None:
+            return self
+        res = instance.__dict__[self.func.__name__] = self.func(instance)
+        return res
+
+class Thing(object):
+    @cached_property
+    def four(self):
+        print "four"
+        return 4
+
+thing = Thing()
+print thing.four()
+print thing.four()
\ No newline at end of file