liveBook · Manning

Lazy initialization

Lazy initialization is a technique that postpones creating, retrieving, computing, or loading a value or resource until it is needed.

How 3 Manning books explain it

Machine Learning Platform Engineering

Lazy initialization is a pattern that postpones expensive resource creation or setup until the resource is first needed.

How it is used
Defers expensive RAG resource setup while allowing a reusable service object to be created quickly.
How deep it goes
Works through asynchronous setup and readiness checks, without detailing resource-loading internals.
Examples
Uses a RAG class that loads a FAISS index and initializes a Gemini model when a Chainlit chat starts.
What you can do after
Build it from scratch as an asynchronously initialized RAG component with a guard against premature queries.

Read how Machine Learning Platform Engineering explains lazy initialization →

Python How-To

A lazy attribute is an attribute whose value is computed only when the attribute is accessed rather than when its object is initialized.

How it is used
Defers expensive per-object data loading until that data is requested.
How deep it goes
Works through two caching implementations and explains the simplified __dict__ and __getattr__ fallback; does not detail the full attribute-resolution order.
Examples
Shows followers whose remote profiles are fetched on first access, then reused on a second access.
What you can do after
Build it from scratch using a cached property or __getattr__, preferring the explicit property approach.

Read how Python How-To explains lazy initialization →

C++ Concurrency in Action, Second Edition

Lazy initialization is a technique that delays constructing or initializing a resource until it is first required.

How it is used
Delays constructing an expensive shared resource until needed while addressing safe initialization across threads.
How deep it goes
Compares mutex protection, double-checked locking, and std::call_once, while deferring full memory-model treatment.
Examples
Uses an expensive resource, a shared_ptr to some_resource, and a class connection as examples.
What you can do after
Apply std::call_once with std::once_flag to initialize a shared resource or class connection before use.

Read how C++ Concurrency in Action, Second Edition explains lazy initialization →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.