from naas_abi_core.utils.LazyLoader import LazyLoaderdef load_data(): print("loading...") return {"a": 1, "b": 2}lazy = LazyLoader(load_data)print(lazy.is_loaded()) # Falseprint(lazy["a"]) # triggers load, prints "loading...", then 1print(lazy.is_loaded()) # Trueprint(repr(lazy)) # uses cached value
Caveats
Accessing repr(lazy) will load the value (because __repr__ triggers loading).
Only a subset of operations are proxied (__getattr__, iteration, length, indexing, repr). Other operations may not behave like the underlying value unless they route through these methods.