libb.lazydict
- class lazydict[source]
Bases:
attrdictA dictionary where function values are lazily evaluated.
Functions stored as values are called with the dictionary as argument when the key is accessed, making them behave like computed properties.
Basic Usage:
>>> a = lazydict(a=1, b=2, c=lambda x: x.a+x.b) >>> a.c 3 >>> a.a = 99 >>> a.c # Recalculated with new value 101 >>> a.z = 1 >>> a.z 1
Instance Isolation (descriptors are not shared between instances):
>>> z = lazydict(a=2, y=4, f=lambda x: x.a*x.y) >>> z.b Traceback (most recent call last): ... AttributeError: b >>> z.c Traceback (most recent call last): ... AttributeError: c >>> z.f 8 >>> a.f Traceback (most recent call last): ... AttributeError: f