InitHookContext#

class brainstate.InitHookContext(operation, state_ref, timestamp=<factory>, metadata=<factory>, value=None, init_metadata=<factory>)[source]#

Context for initialization hooks.

This context is passed to hooks registered for ‘init’ operations (when a State instance is created). It provides the initial value and any initialization metadata.

Init hooks can inspect the initial state and perform actions like logging, validation, or registration, but cannot modify the initial value or cancel the initialization (use factory functions for that).

value#

The initial value of the state

metadata#

Dictionary of initialization metadata (name, tag, etc.)

Examples

>>> def log_state_creation(ctx: InitHookContext):
...     print(f"Created state '{ctx.state_name}' with value: {ctx.value}")
>>> state = brainstate.State(jnp.array([1, 2, 3]), name="my_state")
>>> state.register_hook('init', log_state_creation)