PyREx

pyrex.internal_functions.LazyMutableClass

class pyrex.internal_functions.LazyMutableClass(static_attributes=None)

Class with lazy properties which may depend on other class attributes.

This class is intended as a base class for any class which desires lazy properties which depend on other attributes and thus may need to be recalculated when the class attributes change. Any lazy properties in this class will be lazily evaluated as usual until one of the given static attributes changes, at which point all lazy properties will be cleared and will be recalculated on their next call. By default the static attributes of the class will be set to all attributes present at the time of the LazyMutableClass.__init__ call.

Parameters
static_attributesNone or sequence of str, optional

Set of attribute names on which the lazy properties depend. If None then it will contain all members of __dict__ at the time of the call.

See also

lazy_property

Decorator for lazily-evaluated properties.

Examples

>>> from time import sleep
>>> class A(LazyMutableClass):
...     def __init__(self, value):
...         self.value = value
...         super().__init__()
...     @lazy_property
...     def twice(self):
...         sleep(5)
...         return self.value*2
>>> a = A(1)
>>> "_lazy_twice" in a.__dict__
False
>>> a.twice
2
>>> "_lazy_twice" in a.__dict__
True
>>> a.twice
2
>>> a.value = 5
>>> "_lazy_twice" in a.__dict__
False
>>> a.twice
10
>>> "_lazy_twice" in a.__dict__
True
>>> a.twice
10

PyREx

A Python package for simulation of neutrinos and radio antennas in ice. Version 1.10.0

Navigation