API Reference
materialized

def materialized(obj: Memo, /) -> Memo

Provides a reference to a Memo object that is safe to use within its own __init__ method.

Normally, an object isn't fully registered and available in the dbzero system until its __init__ method completes. Trying to perform operations on self from within the constructor—like adding it to an index or obtaining its UUID—can lead to errors because the object doesn't "exist" yet from the perspective of other components.

The dbzero.materialized() function solves this problem by telling dbzero to allocate the necessary internal structures for the object immediately, returning a stable handle that can be used right away. This allows you to build composite, self-referential data structures during object creation.

Parameters

  • obj Memo
    The object instance (typically self) being initialized.

Returns

A stable handle to the object that can be used with other dbzero functions.


Examples

Indexing an object during initialization

You can add an object to a collection from its own constructor by wrapping self with dbzero.materialized.

import dbzero as db0
 
@db0.memo
class Document:
  def __init__(self, content, mapping):
    self.content = content
    # Add this new document to the mapping immediately
    mapping[content] = db0.materialized(self)
 
# Usage
my_dict = db0.dict()
doc1 = Document("Hello dbzero", my_dict)
 
assert my_dict["Hello dbzero"] is doc1

Obtaining a UUID during initialization

Similarly, you can obtain a persistent UUID for an object inside its __init__ method.

import dbzero as db0
 
@db0.memo
class User:
  def __init__(self):
    # Assign a unique, persistent ID to the user upon creation
    self.id = db0.uuid(db0.materialized(self))
 
user = User()
print(f"User created with ID: {user.id}")
 
# The ID is stable and can be used for fetching
fetched_user = db0.fetch(user.id)
assert user is fetched_user