Permalink
Browse files

property

  • Loading branch information...
1 parent 6114e3a commit c190d73b36e8a567932b2eb0a43f3fa3c9122e13 @albertz committed Jul 31, 2012
Showing with 22 additions and 0 deletions.
  1. +22 −0 py-descr-property.py
View
@@ -28,3 +28,25 @@ class B:
print "b.x:", b.x
print b.__class__.x
+
+# ---
+
+class initBy(property):
+ def __init__(self, initFunc):
+ property.__init__(self, fget = self.fget)
+ self.initFunc = initFunc
+ def fget(self, inst):
+ if hasattr(self, "value"): return self.value
+ self.value = self.initFunc()
+ return self.value
+
+def initFuncTest():
+ print "initFuncTest"
+ return "x"
+
+class C:
+ x = initBy(initFuncTest)
+
+c = C()
+print "c.x:", c.x
+print "c.x:", c.x

0 comments on commit c190d73

Please sign in to comment.