lazyproperty¶
-
class
astropy.utils.decorators.
lazyproperty
(fget, fset=None, fdel=None, doc=None)[source] [edit on github]¶ Bases:
object
Works similarly to property(), but computes the value only once.
This essentially memorizes the value of the property by storing the result of its computation in the
__dict__
of the object instance. This is useful for computing the value of some property that should otherwise be invariant. For example:>>> class LazyTest(object): ... @lazyproperty ... def complicated_property(self): ... print('Computing the value for complicated_property...') ... return 42 ... >>> lt = LazyTest() >>> lt.complicated_property Computing the value for complicated_property... 42 >>> lt.complicated_property 42
As the example shows, the second time
complicated_property
is accessed, theprint
statement is not executed. Only the return value from the first access offcomplicated_property
is returned.If a setter for this property is defined, it will still be possible to manually update the value of the property, if that capability is desired.
Adapted from the recipe at http://code.activestate.com/recipes/363602-lazy-property-evaluation
Methods Summary
deleter
(fdel)getter
(fget)setter
(fset)Methods Documentation
-
deleter
(fdel)[source] [edit on github]¶
-
getter
(fget)[source] [edit on github]¶
-
setter
(fset)[source] [edit on github]¶
-