[Zope3-checkins]
SVN: Zope3/trunk/src/zope/cachedescriptors/property.
Added a simple "read" property.
Jim Fulton
jim at zope.com
Thu Sep 29 05:12:37 EDT 2005
Log message for revision 38680:
Added a simple "read" property.
Changed:
U Zope3/trunk/src/zope/cachedescriptors/property.py
U Zope3/trunk/src/zope/cachedescriptors/property.txt
-=-
Modified: Zope3/trunk/src/zope/cachedescriptors/property.py
===================================================================
--- Zope3/trunk/src/zope/cachedescriptors/property.py 2005-09-29 06:57:59 UTC (rev 38679)
+++ Zope3/trunk/src/zope/cachedescriptors/property.py 2005-09-29 09:12:36 UTC (rev 38680)
@@ -70,3 +70,16 @@
inst.__dict__[name] = value
return value
+
+class readproperty(object):
+
+ def __init__(self, func):
+ self.func = func
+
+ def __get__(self, inst, class_):
+ if inst is None:
+ return self
+
+ func = self.func
+ return func(inst)
+
Modified: Zope3/trunk/src/zope/cachedescriptors/property.txt
===================================================================
--- Zope3/trunk/src/zope/cachedescriptors/property.txt 2005-09-29 06:57:59 UTC (rev 38679)
+++ Zope3/trunk/src/zope/cachedescriptors/property.txt 2005-09-29 09:12:36 UTC (rev 38680)
@@ -126,3 +126,30 @@
>>> '%.2f' % point.diameter
computing diameter
'5.66'
+
+readproperties
+==============
+
+readproperties are like lazy computed attributes except that the
+attribute isn't set by the property:
+
+
+ >>> class Point:
+ ...
+ ... def __init__(self, x, y):
+ ... self.x, self.y = x, y
+ ...
+ ... def radius(self):
+ ... print 'computing radius'
+ ... return math.sqrt(self.x**2 + self.y**2)
+ ... radius = property.readproperty(radius)
+
+ >>> point = Point(1.0, 2.0)
+
+ >>> '%.2f' % point.radius
+ computing radius
+ '2.24'
+
+ >>> '%.2f' % point.radius
+ computing radius
+ '2.24'
More information about the Zope3-Checkins
mailing list