lazyproperty
paper-pptx 0.2.0 API reference
Decorator like @property, but evaluated only on first access.
Like @property, this can only be used to decorate methods having only a self parameter, and
is accessed like an attribute on an instance, i.e. trailing parentheses are not used. Unlike
@property, the decorated method is only evaluated on first access; the resulting value is
cached and that same value returned on second and later access without re-evaluation of the
method.
Like @property, this class produces a data descriptor object, which is stored in the dict of the class under the name of the decorated method ('fget' nominally). The cached value is stored in the dict of the instance under that same name.
Because it is a data descriptor (as opposed to a non-data descriptor), its __get__() method
is executed on each access of the decorated attribute; the dict item of the same name is
"shadowed" by the descriptor.
While this may represent a performance improvement over a property, its greater benefit may be its other characteristics. One common use is to construct collaborator objects, removing that "real work" from the constructor, while still only executing once. It also de-couples client code from any sequencing considerations; if it's accessed from more than one location, it's assured it will be ready whenever needed.
Loosely based on: https://stackoverflow.com/a/6849299/1902513.
A lazyproperty is read-only. There is no counterpart to the optional "setter" (or deleter) behavior of an @property. This is critically important to maintaining its immutability and idempotence guarantees. Attempting to assign to a lazyproperty raises AttributeError unconditionally.
The parameter names in the methods below correspond to this usage example::
class Obj(object)
@lazyproperty def fget(self): return 'some result'
obj = Obj()
Not suitable for wrapping a function (as opposed to a method) because it is not callable.
lazyproperty(fget: Callable[..., _T])