
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1540 fd906abe-77d9-0310-91a1-e0d9ade77398
54 lines
1.2 KiB
Text
54 lines
1.2 KiB
Text
================
|
|
Smart Properties
|
|
================
|
|
|
|
$Id$
|
|
|
|
lzprop
|
|
======
|
|
|
|
The `lzprop` decorator allows the declaration of lazy properties - attributes
|
|
that are calculated only on first access, and then just once. This is
|
|
extremely useful when working with objects of a limited lifetime (e.g.
|
|
adapters) that provide results from expensive calculations.
|
|
|
|
We use a simple class with one lazy property that tracks the calculation
|
|
by printing an informative message:
|
|
|
|
>>> from cybertools.util.property import lzprop
|
|
|
|
>>> class Demo(object):
|
|
... base = 6
|
|
... @lzprop
|
|
... def value(self):
|
|
... print 'calculating'
|
|
... return self.base * 7
|
|
|
|
>>> demo = Demo()
|
|
|
|
When we first access the `value` attribute the corresponding method will be
|
|
called:
|
|
|
|
>>> demo.value
|
|
calculating
|
|
42
|
|
|
|
On subsequent accesses the previously calculated value will be returned:
|
|
|
|
>>> demo.value
|
|
42
|
|
>>> demo.base = 15
|
|
>>> demo.value
|
|
42
|
|
|
|
Let's make sure the value is really calculated upon first access (and not
|
|
already during compilation or object creation):
|
|
|
|
>>> demo2 = Demo()
|
|
>>> demo2.base = 15
|
|
>>> demo2.value
|
|
calculating
|
|
105
|
|
>>> demo2.value
|
|
105
|
|
|